Python in Excel: How to check Benford’s Law with Copilot
Want to share your content on python-bloggers? click here.
Numbers don’t spread out evenly across digits. In many real datasets, the leading digit “1” shows up about 30% of the time while “9” hardly appears. That’s Benford’s Law. If your numbers don’t follow this pattern when they should, it could mean something odd or even fraudulent is going on.
The reason comes from scale. When values stretch across different magnitudes like dollars, populations, or GDPs, lower digits cover more of the number line. That makes “1” appear more often than “9.”
Excel users should care because this is a quick way to sanity check data. Doing it in plain Excel is messy, but with Copilot and Python in Excel you can generate the code, run the test, and see results right in the sheet.
We’ll be working with GDP data from the World Bank. It spans everything from tiny island nations to the biggest economies, which makes it a perfect test case for Benford’s Law. Download the exercise file below to follow along:
Frequency counts of first digit
To check Benford’s Law, I asked Copilot:
“Using GDP data, extract the first digit from each country’s GDP value. Count how frequently each digit from 1 to 9 occurs as the first digit.”

Copilot returned a simple frequency table. The digit 1 appeared 69 times as the first digit, far more than any other. The counts then tapered off, with 2 showing up 50 times, and by the time we reach 9, it only appeared 8 times.
That “long tail” is exactly what Benford’s Law predicts: in many naturally occurring datasets, smaller leading digits like 1s and 2s are far more common than larger ones like 8s and 9s. Why? Because numbers grow exponentially across orders of magnitude. A country’s GDP is just as likely to fall between 10 and 20 billion as between 80 and 90 billion, but the first digit in those ranges will be very different.
This quick count shows the GDP data behaving as expected. If we saw a flat or unusual distribution, that would be a red flag. Analysts sometimes use this property of Benford’s Law to detect anomalies or even fraud in financial reporting.
Visualizing the actual distribution versus Benford’s Law
Once I had Copilot tally up the first digits, I asked it to take the next step:
“Calculate the percentage distribution of these first digits and compare them visually against the expected distribution according to Benford’s Law.”

Copilot generated a side-by-side chart. The blue bars show the observed GDP data; the orange bars are the theoretical Benford’s Law distribution. Notice how closely they track. About 30% of GDP figures start with the digit 1, while fewer than 5% start with 9: almost a perfect fit with Benford’s prediction.
Interpreting the results
To push the analysis further, I asked Copilot to highlight where the GDP data deviates from Benford’s Law.
“Highlight significant deviations between actual first-digit frequency versus Benford’s Law expected frequency, if any. Suggest possible interpretations or concerns from a business auditing perspective.”
Copilot’s commentary pointed out where the GDP data seemed to deviate from Benford’s Law… a few too many 2s and 4s, a few too few 8s and 9s. But are those differences actually meaningful, or just random noise in the dataset?
Testing for statistical significance
That’s where a statistical test comes in. I asked Copilot to run a chi-square test to formally compare the observed distribution against Benford’s Law.
“Perform a statistical test (e.g., chi-square) to quantify the deviation from Benford’s Law.”

The result was a chi-square statistic of about 11.44 with a p-value of 0.18. Since that’s well above the usual 0.05 threshold, we don’t have evidence that the GDP data significantly deviates from Benford’s prediction. In other words, the small bumps we saw in the bar chart are well within the range of normal variation.
Conclusion
So what does all this mean? Benford’s Law isn’t just a neat math curiosity. It’s a practical lens for spotting when numbers might not add up. With Copilot and Python in Excel, you can go from raw data to statistical tests without leaving your spreadsheet.
For auditors, that means a quick way to flag suspicious ledgers. For analysts, it’s a sanity check on whether a dataset behaves like it should. And for anyone working with large, messy numbers, it’s a reminder that the patterns in data can be just as important as the numbers themselves.
The takeaway: if your digits line up with Benford’s Law, good… you can have more confidence in your dataset. If they don’t, that’s not proof of fraud, but it is a signal worth investigating. Either way, Excel now gives you the power to test it yourself with just a few prompts.
The post Python in Excel: How to check Benford’s Law with Copilot first appeared on Stringfest Analytics.
Want to share your content on python-bloggers? click here.