Skip to content

Instantly share code, notes, and snippets.

@tdalon
Last active April 25, 2024 06:18
Show Gist options
  • Save tdalon/c7454ae67bc7c11f6e483bb113e0f6ed to your computer and use it in GitHub Desktop.
Save tdalon/c7454ae67bc7c11f6e483bb113e0f6ed to your computer and use it in GitHub Desktop.
Jira to PowerBI
// function GetItems - recursive implementation
(previousTable as nullable table,Fields as text) =>
let
start = if (previousTable = null) then 0 else Table.RowCount(previousTable),
maxRes = if (maxTotal=null) or (start + maxResults < maxTotal) then maxResults else maxTotal - start ,
restUrl = JiraRootUrl&"/rest/api/2/search?jql=filter="&Text.From(JiraFilterId)&"&maxResults="&Text.From(maxRes)&"&startAt="&Text.From(start),
restUrl2 = if Fields <> "" then restUrl & "&fields="&Fields else restUrl,
Source = Json.Document(Web.Contents(restUrl2)),
ChunkTable = Table.FromList(Source[issues], Splitter.SplitByNothing(), {"Column1"}),
//ChunkTable = Table.FromList(Source[issues], Splitter.SplitByNothing(), null, null, ExtraValues.Error),
//#"Expanded Column1" = Table.ExpandRecordColumn(ChunkTable, "Column1", {"key", "fields"}, {"Column1.key", "Column1.fields"})
table1 = if previousTable = null then ChunkTable else Table.Combine({previousTable,ChunkTable}),
ItemsTable = if (Table.RowCount(ChunkTable)< maxRes) or (maxRes = maxTotal-start) then table1 else @GetItems(table1,Fields)
in
ItemsTable
// STEP GetIssuesTable
let
// Get fields url parameters (txt CSV) from Filter definition (uses JiraFilterId) or user parameter JiraFields
FieldsText = if JiraFields <> null then JiraFields else
let
restUrl = JiraRootUrl & "/rest/api/2/filter/" & Text.From(JiraFilterId) & "/columns",
response = Web.Contents(restUrl,
[ManualStatusHandling={404}]),
responseMetadata = Value.Metadata(response),
responseStatus = responseMetadata[Response.Status],
Columns = if responseStatus =404 then "" else
let
FilterColumns = Json.Document(response),
#"Converted to Table" = Table.FromList(FilterColumns, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"label", "value"}, {"label", "value"}),
//Columns2 = Table.RemoveMatchingRows(#"Expanded Column1",{[label="Key"]},"label") // remove Key because not exported as field
Fields = Text.Combine(#"Expanded Column1"[value],",")
in
Fields
in
Columns,
// Call function GetItems
IssuesTable = GetItems(null,FieldsText)
in
IssuesTable
// STEP Expand&Rename
let
IssuesTable = GetIssuesTable,
ExpIssues = Table.ExpandRecordColumn(IssuesTable, "Column1", {"key", "fields"}, {"Key", "Fields"}),
// expand fields s. https://exceed.hr/blog/dynamically-expand-table-or-record-columns-in-power-query/
IssuesRaw = Table.ExpandRecordColumn(ExpIssues, "Fields", Record.FieldNames(ExpIssues{0}[Fields]),Record.FieldNames(ExpIssues{0}[Fields]) ),
// extracting field info
restUrl = JiraRootUrl&"/rest/api/2/field",
FieldsList= Json.Document(Web.Contents(restUrl)),
#"Converted to Table" = Table.FromList(FieldsList, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
FieldsIdNameTable = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"id", "name"}, {"id", "name"}),
Issues = Table.RenameColumns(IssuesRaw,Table.ToRows(FieldsIdNameTable),MissingField.Ignore),
// rename using https://learn.microsoft.com/en-us/powerquery-m/table-renamecolumns and MissingField.Ignore
#"Expanded Issue Type" = if not(Table.HasColumns(Issues,"Issue Type")) then Issues else
Table.ExpandRecordColumn(Issues, "Issue Type", {"name"}, {"Issue Type"}),
#"Expanded Status" = if not(Table.HasColumns(Issues,"Issue Type")) then #"Expanded Issue Type" else
Table.ExpandRecordColumn(#"Expanded Issue Type", "Status", {"name"}, {"Status"}),
#"Expanded Risk Probability" = if not(Table.HasColumns(Issues,"Risk probability")) then #"Expanded Status" else
Table.ExpandRecordColumn(#"Expanded Status", "Risk probability", {"value"}, {"Risk probability"}),
#"Expanded Risk Consequence" = if not(Table.HasColumns(Issues,"Risk consequence")) then #"Expanded Risk Probability" else
Table.ExpandRecordColumn(#"Expanded Risk Probability", "Risk consequence", {"value"}, {"Risk consequence"}) ,
#"Expanded Assignee" = if not(Table.HasColumns(Issues,"Assignee")) then #"Expanded Risk Consequence" else
Table.ExpandRecordColumn(#"Expanded Risk Consequence", "Assignee", {"name"}, {"Assignee"}),
#"Expanded Reporter" = if not(Table.HasColumns(Issues,"Reporter")) then #"Expanded Assignee" else
Table.ExpandRecordColumn(#"Expanded Assignee", "Reporter", {"name"}, {"Reporter"})
in
#"Expanded Reporter"
@tdalon
Copy link
Author

tdalon commented May 10, 2021

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