Forum Discussion
Benford's law , Power query
I am new with power query, I need some help in figuring out the way to use this law in power query
I have a Transaction amount, I want to run Benford's law to find out the frequency and distribution of digits in the last two numbers of the transaction amount.
Hi
please paste this code into the advanced editor and follow the steps:
let MyTable = Table.FromColumns({List.Transform(List.Random(10000), each _ * 10000) } ), ExtractLast2Digits = Table.AddColumn(MyTable, "Last2Digits", each Text.End(Text.From([Column1]),2)), BenfordTable = Table.AddColumn( #table( {"Last2Digits"}, List.Transform( {0..99}, each {Text.PadStart(Text.From(_), 2, "0")})), "BenfordNumber", each 0.01 as number ), #"Merged Queries" = Table.NestedJoin(BenfordTable, {"Last2Digits"}, ExtractLast2Digits, {"Last2Digits"}, "MyTable", JoinKind.LeftOuter), #"Aggregated MyTable" = Table.AggregateTableColumn(#"Merged Queries", "MyTable", {{"Column1", each List.Count(_) / Table.RowCount(MyTable), "ActualDistribution"}}), #"Inserted Subtraction" = Table.AddColumn(#"Aggregated MyTable", "Deviation", each [ActualDistribution] - [BenfordNumber], type number), #"Inserted Absolute Value" = Table.AddColumn(#"Inserted Subtraction", "Absolute Deviation", each Number.Abs([Deviation]), type number) in #"Inserted Absolute Value"It will create a table with the Benford-distribution values.
If you want to apply it to your data, just replace the code in the first step (MyTable) by a reference to your table and make sure that that column with the values to be analyzed is called "Column1". Then you don't have to adjust anything further in the code.
8 Replies
- ImkeF
Community Champion
Hi
please paste this code into the advanced editor and follow the steps:
let MyTable = Table.FromColumns({List.Transform(List.Random(10000), each _ * 10000) } ), ExtractLast2Digits = Table.AddColumn(MyTable, "Last2Digits", each Text.End(Text.From([Column1]),2)), BenfordTable = Table.AddColumn( #table( {"Last2Digits"}, List.Transform( {0..99}, each {Text.PadStart(Text.From(_), 2, "0")})), "BenfordNumber", each 0.01 as number ), #"Merged Queries" = Table.NestedJoin(BenfordTable, {"Last2Digits"}, ExtractLast2Digits, {"Last2Digits"}, "MyTable", JoinKind.LeftOuter), #"Aggregated MyTable" = Table.AggregateTableColumn(#"Merged Queries", "MyTable", {{"Column1", each List.Count(_) / Table.RowCount(MyTable), "ActualDistribution"}}), #"Inserted Subtraction" = Table.AddColumn(#"Aggregated MyTable", "Deviation", each [ActualDistribution] - [BenfordNumber], type number), #"Inserted Absolute Value" = Table.AddColumn(#"Inserted Subtraction", "Absolute Deviation", each Number.Abs([Deviation]), type number) in #"Inserted Absolute Value"It will create a table with the Benford-distribution values.
If you want to apply it to your data, just replace the code in the first step (MyTable) by a reference to your table and make sure that that column with the values to be analyzed is called "Column1". Then you don't have to adjust anything further in the code.
- Ethanhunt123
Helper IV
This is only for the last digit I have modified the code. 2 does not fall in the last place of any number but it is still showing the distribution
- ImkeF
Community Champion
- AnonymousNot applicable
I keep on getting a circular reference error when tyring to apply it to my data
- ImkeF
Community Champion
Hi Anonymous ,
I would need to see your M-code on how you applied it to help you here.
If you copy and paste the code I've provided you should see everything working fine.But maybe you prefer a DAX-solution that has some advantages instead: Dynamic Benford's Law measures in Power BI and Power Pivot – (thebiccountant.com)
- jina_jhonsonNew Member
Interesting point on how Benford’s Law can surface anomalies, especially when paired with Power Query for quick pattern checks. For anyone exploring real-world datasets, working with structured legal data can be a practical use case. Resources can offer consistent numerical records to test distributions and assumptions. Applying these techniques to such data can sharpen both analytical and data-cleaning skills.
- andrew365New Member
If you’re analyzing patterns like Benford’s Law in your data, it’s crucial to cross-check results with official sources for accuracy. I found that having access to detailed https://stclair-countycourts.org can provide valuable insights when handling real-world datasets or validating anomalies. Combining this with Power Query makes it easier to organize and interpret large numbers efficiently. Always double-check your methodology to ensure reliable conclusions.