Skip to content

Instantly share code, notes, and snippets.

@tdalon
Last active May 10, 2021 07:06
Show Gist options
  • Save tdalon/8086c4968c057ceb1a880e9f38add735 to your computer and use it in GitHub Desktop.
Save tdalon/8086c4968c057ceb1a880e9f38add735 to your computer and use it in GitHub Desktop.
Import Jira Filter to PowerBI (with Automatic Fields renaming) - List based approach
// STEP: GetTotal
let
// Get total
Source = Json.Document(Web.Contents(JiraRootUrl&"/rest/api/2/search?jql=filter="&JiraFilterId&"&maxResults=1")),
total = if maxTotal = null then Source[total] else List.Min({maxTotal,Source[total]})
in
total
// STEP: GetFilterColumns
let
// Get list of fields from Filter definition
FilterColumns = Json.Document(Web.Contents(JiraRootUrl&"/rest/api/2/filter/"&JiraFilterId&"/columns")),
#"Converted to Table" = Table.FromList(FilterColumns, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"label", "value"}, {"label", "value"})
in
#"Expanded Column1"
// STEP: GetFields
let
// Get list of fields from Filter definition
Fields = if JiraFields <> null then JiraFields else
let
FilterColumnsTable = GetFilterColumns,
Fields2 = Text.Combine(FilterColumnsTable[value],", ")
in
Fields2
in
Fields
// STEP (function): StartAt
(start as number) =>
let
maxRes = if (maxTotal=null) or (start + maxResults < maxTotal) then maxResults else maxTotal - start ,
Fields = GetFields,
Source = Json.Document(Web.Contents(JiraRootUrl&"/rest/api/2/search?jql=filter="&JiraFilterId&"&maxResults="&Text.From(maxRes)&"&fields=["&Fields&"]"&"&startAt="&Text.From(start))),
issues = Source[issues],
#"Converted to Table" = Table.FromList(issues, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"key", "fields"}, {"Column1.key", "Column1.fields"})
in
#"Expanded Column1"
// STEP: GetIssues
let
total = GetTotal,
cnt = Number.RoundUp(total/maxResults) ,
total1 = List.Numbers(0,cnt,maxResults),
#"Converted to Table" = Table.FromList(total1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Column1", Int64.Type}}),
#"Invoked Custom Function" = Table.AddColumn(#"Changed Type", "Data", each StartAt([Column1])),
#"Removed Columns" = Table.RemoveColumns(#"Invoked Custom Function",{"Column1"}),
#"Expanded Data" = Table.ExpandTableColumn(#"Removed Columns", "Data", {"Column1.key", "Column1.fields"}, {"Column1.key", "Column1.fields"}),
Issues = if (JiraFields = null) then
let
FilterColumnTable = GetFilterColumns,
IssuesRenamed = Table.ExpandRecordColumn(#"Expanded Data", "Column1.fields", FilterColumnTable[value],FilterColumnTable[label] )
in
IssuesRenamed
else
let
Fields = GetFields,
FieldsList = Text.Split(Fields,","),
IssuesNoRenamed = Table.ExpandRecordColumn(#"Expanded Data", "Column1.fields", FieldsList,FieldsList )
//#"Expanded status" = Table.ExpandRecordColumn(#"Expanded fields", "status", {"name"}, {"status.name"}),
//#"Expanded project" = Table.ExpandRecordColumn(#"Expanded status", "project", {"name"}, {"project.name"})
in
IssuesNoRenamed
in
Issues
@tdalon
Copy link
Author

tdalon commented May 7, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment