-
-
Save PBI-DataVizzle/e60bebae165c0d7bf648998fc6a62c57 to your computer and use it in GitHub Desktop.
M Query Gists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This gist containt useful pieces of M Query code |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= Table.SelectRows(#"Previous Step", each | |
List.Contains( | |
{ | |
"Blue", | |
"Orange", | |
"Purple" | |
},[Column to be filtered] | |
)=true | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let | |
Source = Json.Document( | |
Web.Contents( | |
"http://worldtimeapi.org/api/timezone/", | |
[ | |
RelativePath = | |
"/Europe"& | |
"/Zurich" | |
] | |
) | |
), | |
datetime = Source[datetime], | |
#"Converted to Table" = #table({"Last Refreshed Time"}, {{datetime}}), | |
#"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Last Refreshed Time", type datetimezone}}) | |
in | |
#"Changed Type" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Step 1. Create this function in a new blank query with the name "CleanFX". | |
(mytext as text)=> | |
let | |
//this line is for testing purposes | |
//mytext = "University of abroad", | |
Source = mytext, | |
//This step replaces characters | |
#"ReplaceMatchingChar" = Text.Combine( | |
List.ReplaceMatchingItems( | |
Text.ToList (Source), { | |
{"ü", "u"}, | |
{"ö", "o"} | |
} | |
) | |
), | |
//This step replaces words | |
#"ReplaceMatchingWord" = Text.Combine( | |
List.ReplaceMatchingItems( | |
Text.Split(#"ReplaceMatchingChar"," "), | |
{ | |
{"fo", "of"}, | |
{"teh", "the"} | |
} | |
)," " | |
), | |
//This step removes characters | |
#"Cleaned" = | |
Table.TransformColumns( | |
#table(1, {{#"ReplaceMatchingWord"}}), | |
{{ | |
"Column1", | |
each | |
List.Accumulate(Text.ToList(",."),_,(String,Remove) => Text.Replace(String,Remove,"")) | |
}} | |
), | |
#"Trimmed Text" = Table.TransformColumns(Cleaned,{{"Column1", Text.Trim, type text}}), | |
//This step replaces full cell contents | |
#"ReplaceFullContent" = List.ReplaceMatchingItems( | |
{#"Trimmed Text"{0}[Column1]}, | |
{ | |
{"University of Zurich","UZH"}, | |
{"University of the Abroad","University abroad"} | |
} | |
), | |
#"Final Text" = #"ReplaceFullContent"{0} | |
in | |
#"Final Text" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Step 2. Add this line of code to the column that needs to be cleaned. | |
#"CleanFx" = Table.TransformColumns(Previous_Step, {{"Column Name to be cleaned", each CleanFx(_)}}) | |
in | |
#"CleanFx" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Thanks Chris Webb for this useful patttern. https://blog.crossjoin.co.uk/2015/05/11/nested-calculations-in-power-query/ | |
//This is my post on the practical applications of Chris' code https://oscarvalerock.medium.com/practical-application-of-nested-calculations-in-power-query-bb52e6750690 | |
#"Grouped Rows" = Table.Group(#"Previous Step", {"Column to be grouped by"}, {{"AllRows", each _, type table [ID=nullable number, First rotation=nullable text]}}), | |
RankFunction = (tabletorank as table) as table => | |
let | |
SortRows = Table.Sort(tabletorank,{{"Completion time", Order.Descending}}), | |
AddIndex = Table.AddIndexColumn(SortRows, "Rank", 1, 1), | |
SelectRows= Table.SelectRows(AddIndex, each ([Rank] = 1)) | |
in | |
SelectRows, | |
#"Add Rank" = Table.TransformColumns(#"Grouped Rows" , {"AllRows", each RankFunction(_)}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Find how to use the code in my blog post. | |
//https://www.bibb.pro/post/azure-devops-power-bi-report | |
let | |
Source = OData.Feed("https://analytics.dev.azure.com/{OrganizationName}/{ProjectName}/_odata/v2.0/WorkItems?" | |
//Filters | |
&"$filter=WorkItemType eq 'Epic'" | |
//Fields that do not need to be expanded | |
&"&$select= | |
WorkItemId, | |
WorkItemType, | |
Title, | |
State, | |
ParentWorkItemId, | |
TagNames" | |
//Fields that need to be expanded | |
&"&$expand= | |
AssignedTo( | |
$select= | |
UserName, | |
UserEmail | |
) | |
", | |
null, [Implementation="2.0",OmitValues = ODataOmitValues.Nulls,ODataVersion = 4]), | |
#"Expanded AssignedTo" = Table.ExpandRecordColumn(Source, "AssignedTo", {"UserName", "UserEmail"}, {"UserName", "UserEmail"}) | |
in | |
#"Expanded AssignedTo" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(workItemId as number) => | |
let | |
// workItemId = "2", | |
#"Token" = Binary.ToText(Text.ToBinary("{email}:" & {Personal Access Token})), | |
Source = Json.Document( | |
Web.Contents( | |
"https://dev.azure.com/"& {OrganizationName}/{ProjectName}, | |
[ | |
RelativePath = "/_apis/wit/workitems/" & Number.ToText(workItemId) & "?api-version=5.0&fields=System.Description", | |
Headers = [ | |
#"Authorization" = "Basic " & #"Token" | |
] | |
] | |
) | |
) | |
in | |
Source |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(workItemID as number) => | |
let | |
#"Token" = Binary.ToText(Text.ToBinary("{email}:" & {Personal Access Token})), | |
Source = Json.Document( | |
Web.Contents( | |
"https://dev.azure.com/"&{OrganizationName}/{ProjectName}&"/_apis/wit/workitems/" & Number.ToText(workItemID) & "/comments?api-version=5.1-preview.3", | |
[ | |
Headers = [ | |
#"Authorization" = "Basic " & #"Token" | |
] | |
] | |
) | |
), | |
comments = Source[comments], | |
#"Converted to Table" = Table.FromList(comments, Splitter.SplitByNothing(), null, null, ExtraValues.Error), | |
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"workItemId", "text", "createdBy"}, {"workItemId", "Comment", "createdBy"}), | |
#"Expanded createdBy" = Table.ExpandRecordColumn(#"Expanded Column1", "createdBy", {"uniqueName"}, {"Comment by"}) | |
in | |
#"Expanded createdBy" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Thanks Zubair_Muhammad | |
//https://community.powerbi.com/t5/Quick-Measures-Gallery/DAX-s-PATH-function-equivalent-Custom-Column-in-Power-Query/m-p/800386 | |
//if Parent = null, you might need to add the step below: | |
// #"Replace Nulls"= Table.ReplaceValue({Previous Step},null, each _[{Child}],Replacer.ReplaceValue,{"{Parent}"}) | |
#"Added Custom" = Table.AddColumn({Previous Step}, "Path", each | |
let //Define your columns below | |
c=[{Child}],p=[{Parent}],mytable={Previous Step},pc="{Parent}",cc="{Child}" | |
in | |
let mylist={c} & List.Generate(()=>[x=0,y=p,w=1],each [w] > 0,each [z=[y], | |
x=Table.Column(Table.SelectRows(mytable,each Record.Field(_,cc)=z),pc),y=x{0},w=List.Count(x) | |
], | |
each [y] | |
) | |
in | |
Text.Combine(List.Reverse(List.RemoveItems( | |
List.Transform(mylist,each Text.From(_)),{null,""})),"|") | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let | |
GetPages = | |
let | |
Json = Json.Document( | |
Web.Contents( | |
"http://api.worldbank.org/v2/", | |
[ | |
RelativePath = "country?format=json" | |
] | |
) | |
), | |
GetPages = Json{0}, | |
GetPages2 = GetPages[#"pages"] | |
in | |
GetPages2, | |
GetPage = (Index) => | |
let | |
Json = Json.Document( | |
Web.Contents( | |
"http://api.worldbank.org/v2/", | |
[ | |
RelativePath = "country?page="&Text.From(Index)&"&format=json" | |
] | |
) | |
), | |
GetPages = Json{1} | |
in | |
GetPages, | |
PageIndices = {1..GetPages}, | |
Pages = List.Transform(PageIndices, each GetPage(_)), | |
UnionPages = List.Union(Pages), | |
#"Converted to Table" = Table.FromList(UnionPages, Splitter.SplitByNothing(), null, null, ExtraValues.Error), | |
#"Expanded Column2" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"id", "iso2Code", "name", "region", "adminregion", "incomeLevel", "lendingType", "capitalCity", "longitude", "latitude"}, {"id", "iso2Code", "name", "region", "adminregion", "incomeLevel", "lendingType", "capitalCity", "longitude", "latitude"}) | |
in | |
#"Expanded Column2" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Function to get the binary content of a folder without need to combine elements | |
// This example is for an Excel file | |
GetContentFx = (filecontent as binary) => | |
let | |
#"Imported Excel Workbook" = Excel.Workbook(filecontent), | |
Table1_Table = #"Imported Excel Workbook"{[Item="Table1",Kind="Table"]}[Data] | |
in | |
Table1_Table | |
#"Add Content" = Table.TransformColumns(#"Previous step" , {"Content", each GetContentFx(_)}) | |
in | |
#"Add Content" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This is the basic ServiceNow request | |
let | |
Source = Json.Document( | |
Web.Contents( | |
"https://{ServiceNowInstance}/api/now/", | |
[ | |
RelativePath="table/{tableName}", //replace {tableName} for the required table e.g. incident, sc_task | |
Headers=[ | |
Accept="application/json", | |
#"content-type"="application/json" | |
] | |
] | |
) | |
), | |
result = Source[result] | |
in | |
result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This ServiceNow request uses the aggregate API (stats endpoint), which will return the number of elements in a table; which may come in handy for paging elements. | |
let | |
Source = | |
Json.Document(Web.Contents("https://{ServiceNowInstance}.service-now.com/api/now/", | |
[ | |
RelativePath="stats/{tableName}", //replace {tableName} for the required table | |
Query=[ | |
sysparm_count="true" //this query paramenter indicates we request the count of items on the table. | |
], | |
Headers=[ | |
Accept="application/json", | |
#"content-type"="application/json" | |
] | |
] | |
)), | |
result = Source[result], | |
stats = result[stats] | |
in | |
stats |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let | |
Source = Json.Document( | |
Web.Contents( | |
"https://{ServiceNowInstance}.service-now.com/api/now/", | |
[ | |
RelativePath = "table/{tableName}", //replace {tableName} for the required table | |
Query=[ | |
sysparm_offset = "0", // used for offsetting the number of results, useful for paging | |
sysparm_limit = "20", //limits the number of results, useful for paging | |
sysparm_display_value = "true", //returns the display or raw value | |
sysparm_query="GOTOstate>=2", //query string used to filter the results | |
sysparm_fields="number,short_description" //list of fields to return in response, bring all if not included | |
], | |
Headers=[ | |
Accept="application/json", | |
#"content-type"="application/json" | |
] | |
] | |
) | |
), | |
result = Source[result], | |
#"Converted to Table" = Table.FromList(result, Splitter.SplitByNothing(), null, null, ExtraValues.Error) | |
in | |
#"Converted to Table" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let | |
picresult = (InputTable as table, InputBinaryZBPosition as number, InputKeyZBPosition as number ) as table => | |
let | |
//Get list of files in folder | |
Source = (InputTable), | |
//Converts table that contians image to list | |
ListToInput = Table.ToRows(Source), | |
//Creates Splitter function | |
SplitTextFunction = Splitter.SplitTextByRepeatedLengths(2000), | |
//Function to convert binary of photo to multiple | |
ConvertOneFile = (InputRow as list) => | |
let | |
BinaryIn = InputRow{InputBinaryZBPosition}, | |
ID = InputRow{InputKeyZBPosition}, | |
//BinaryText = Binary.ToText(BinaryIn, BinaryEncoding.Base64), //use this for png | |
BinaryText = Text.FromBinary(BinaryIn), // use this for svg | |
SplitUpText = SplitTextFunction(BinaryText), | |
AddFileName = List.Transform(SplitUpText, each {ID,_}) | |
in | |
AddFileName, | |
//Loops over all photos and calls the above function | |
ConvertAllRows = List.Transform(ListToInput, each ConvertOneFile(_)), | |
//Combines lists together | |
CombineLists = List.Combine(ConvertAllRows), | |
//Converts results to table | |
ToTable = #table(type table[ID=text,Pic=text],CombineLists), | |
//Adds index column to output table | |
AddIndexColumn = Table.AddIndexColumn(ToTable, "Index", 0, 1) | |
in | |
AddIndexColumn | |
in | |
picresult |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment