Skip to content

Instantly share code, notes, and snippets.

@ralfbecher
Last active August 9, 2018 18:45
Show Gist options
  • Save ralfbecher/5314862 to your computer and use it in GitHub Desktop.
Save ralfbecher/5314862 to your computer and use it in GitHub Desktop.
Calculate Benford's Law in QlikView
//QlikView Script, Load the distribution of first digits, according to Benford's law, as Data Island:
Benford:
LOAD FirstDigit, log10(FirstDigit+1) - log10(FirstDigit) as OccurancePct;
LOAD RecNo() as FirstDigit AutoGenerate(9);
//***********************************************************************************
//Chart
//Dynamic Dimension;
=ValueList(1,2,3,4,5,6,7,8,9)
//Benford's Expression:
=pick(match(ValueList(1,2,3,4,5,6,7,8,9),1,2,3,4,5,6,7,8,9),
Only({<FirstDigit={1}>} OccurancePct),
Only({<FirstDigit={2}>} OccurancePct),
Only({<FirstDigit={3}>} OccurancePct),
Only({<FirstDigit={4}>} OccurancePct),
Only({<FirstDigit={5}>} OccurancePct),
Only({<FirstDigit={6}>} OccurancePct),
Only({<FirstDigit={7}>} OccurancePct),
Only({<FirstDigit={8}>} OccurancePct),
Only({<FirstDigit={9}>} OccurancePct))
//Actual Expression:
=pick(match(ValueList(1,2,3,4,5,6,7,8,9),1,2,3,4,5,6,7,8,9),
count({$<YourField = {"=left(text(fabs(YourField*100000)),1)=1"}>} YourField) / count({$ - <YourField = {0}>} TOTAL left(text(fabs(YourField*100000)),1)),
count({$<YourField = {"=left(text(fabs(YourField*100000)),1)=2"}>} YourField) / count({$ - <YourField = {0}>} TOTAL left(text(fabs(YourField*100000)),1)),
count({$<YourField = {"=left(text(fabs(YourField*100000)),1)=3"}>} YourField) / count({$ - <YourField = {0}>} TOTAL left(text(fabs(YourField*100000)),1)),
count({$<YourField = {"=left(text(fabs(YourField*100000)),1)=4"}>} YourField) / count({$ - <YourField = {0}>} TOTAL left(text(fabs(YourField*100000)),1)),
count({$<YourField = {"=left(text(fabs(YourField*100000)),1)=5"}>} YourField) / count({$ - <YourField = {0}>} TOTAL left(text(fabs(YourField*100000)),1)),
count({$<YourField = {"=left(text(fabs(YourField*100000)),1)=6"}>} YourField) / count({$ - <YourField = {0}>} TOTAL left(text(fabs(YourField*100000)),1)),
count({$<YourField = {"=left(text(fabs(YourField*100000)),1)=7"}>} YourField) / count({$ - <YourField = {0}>} TOTAL left(text(fabs(YourField*100000)),1)),
count({$<YourField = {"=left(text(fabs(YourField*100000)),1)=8"}>} YourField) / count({$ - <YourField = {0}>} TOTAL left(text(fabs(YourField*100000)),1)),
count({$<YourField = {"=left(text(fabs(YourField*100000)),1)=9"}>} YourField) / count({$ - <YourField = {0}>} TOTAL left(text(fabs(YourField*100000)),1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment