SOLVED

Percentiles from frequency table

%3CLINGO-SUB%20id%3D%22lingo-sub-2741999%22%20slang%3D%22en-US%22%3EPercentiles%20from%20frequency%20table%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2741999%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3EThis%20is%20a%20quite%20common%20problem%20to%20solve%20in%20statistics%20it%20seems%20to%20me%2C%20but%20I%20can't%20find%20an%20easy%20solution%20in%20Excel.%20Am%20I%20missing%20something%20or%20is%20this%20just%20something%20that's%20not%20incorporated%3F%3C%2FP%3E%3CP%3EThe%20problem%20is%20simple%3A%20I%20have%20a%20frequency%20table%2C%20and%20want%20to%20calculate%20percentiles.%3C%2FP%3E%3CP%3EEg.%3C%2FP%3E%3CP%3EScore%20(0-5)%20%2F%20Number%20of%20students%3C%2FP%3E%3CP%3E0%20%2F%203%3C%2FP%3E%3CP%3E1%20%2F%202%3C%2FP%3E%3CP%3E2%20%2F%201%3C%2FP%3E%3CP%3E3%20%2F%205%3C%2FP%3E%3CP%3E4%20%2F%206%3C%2FP%3E%3CP%3E5%20%2F%201%3C%2FP%3E%3CP%3ETo%20calculate%20percentiles%2C%20you%20would%20line%20the%20results%20up%20as%20follows%3A%3C%2FP%3E%3CP%3E0%200%200%201%201%202%203%203%203%203%203%204%204%204%204%204%204%205%3C%2FP%3E%3CP%3EYou%20can%20then%20use%20the%20DAX%20PERCENTILE%20function%20(I%20understand%20there%20is%20no%20equivalent%20in%20M)%20on%20such%20an%20%22expanded%22%20list.%3C%2FP%3E%3CP%3EHowever%3A%3C%2FP%3E%3CP%3E1)%20I%20don't%20see%20an%20easy%20way%20to%20convert%20the%20dataset%20into%20the%20%22expanded%22%20dataset%20in%20M%20or%20DAX%20(in%20Excel%20there's%20%3CA%20href%3D%22https%3A%2F%2Fwww.got-it.ai%2Fsolutions%2Fexcel-chat%2Fexcel-tutorial%2Fmedian%2Flearn-how-to-calculate-the-median-from-a-frequency-table-in-excel%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ethis%20%22hack%22%3C%2FA%3E)%3C%2FP%3E%3CP%3E2)%20Even%20if%20I%20would%2C%20in%20real%20life%20the%20expanded%20dataset%20would%20be%20humungous%20-%20and%20this%20only%20to%20come%20to%20a%20few%20crunched%20numbers%20in%20the%20end.%20In%20my%20case%2C%20the%20range%20of%20values%20(left%20column)%20goes%20from%201%20to%2053%20for%2010.000%20products%2C%20and%20the%20frequencies%20(right%20column)%20go%20up%20to%20thousands%20per%20value%2C%20so%20we're%20talking%20about%2010.000%20lists%20of%20100's%20of%20thousands%20of%20numbers%20each.%3C%2FP%3E%3CP%3EI%20found%20a%20mathematical%20explanation%20%3CA%20href%3D%22https%3A%2F%2Fastarmathsandphysics.com%2Fib-maths-notes%2Fprobability-and-statistics%2F1063-quartiles-and-median-from-frequency-tables.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehere%3C%2FA%3E%2C%20but%20it%20doesn't%20seem%20obvious%20to%20me%20to%20get%20this%20done%20in%20Excel%20either...%3C%2FP%3E%3CP%3ESo%20I'm%20hoping%20there's%20a%20formula%20for%20this%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2741999%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPower%20BI%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2742134%22%20slang%3D%22en-US%22%3ERe%3A%20Percentiles%20from%20frequency%20table%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2742134%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F863782%22%20target%3D%22_blank%22%3E%40bartvana%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENot%20sure%20about%20the%20math%2C%20but%20it's%20easy%20to%20repeat%20Excel%20sample.%20For%20such%20table%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20150px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F309716i7FD53EE1D0DF6686%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3Escript%20which%20returns%20median%20(or%200.5%20percentile)%20is%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powerquery%22%3E%3CCODE%3Elet%0A%20%20%20%20Source%20%3D%20Excel.CurrentWorkbook()%7B%5BName%3D%22Table1%22%5D%7D%5BContent%5D%2C%0A%20%20%20%20allValues%20%3D%20Table.AddColumn(%0A%20%20%20%20%20%20%20%20Source%2C%20%22Lists%22%2C%0A%20%20%20%20%20%20%20%20each%20List.Numbers(%5BValue%5D%2C%5BFrequency%5D%2C0))%2C%0A%20%20%20%20Percentile%20%3D%20List.Percentile(%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20or%20List.Median%0A%20%20%20%20%20%20%20%20List.Combine(allValues%5BLists%5D)%2C%200.5)%0Ain%0A%20%20%20%20Percentile%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EPerformance%20could%20be%20an%20issue%2C%20but%20that's%20only%20to%20play%20with%20actual.%20Perhaps%20fixing%20in%20memory%20with%20Index%20column%20or%20Table.Buffer()%20could%20help%20a%20bit.%20Or%20wrap%20above%20with%20List.Buffer().%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2742228%22%20slang%3D%22en-US%22%3ERe%3A%20Percentiles%20from%20frequency%20table%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2742228%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F521%22%20target%3D%22_blank%22%3E%40Sergei%20Baklan%3C%2FA%3E%26nbsp%3BThat's%20genius!%3CBR%20%2F%3EI'm%20trying%20to%20implement%20this%20in%20my%20real%20life%20data%20and%20have%20only%20one%20problem%2C%20the%20grouping%20by%20product.%20Imagine%20you%20would%20have%20sample%20data%20like%20this%20and%20want%20to%20calculate%20the%20median%20by%20product%20(see%20also%20attached%20workbook%20where%20I%20added%20the%20column)%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22bartvana_0-1631359251920.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F309719iAD0A5DF36941D989%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22bartvana_0-1631359251920.png%22%20alt%3D%22bartvana_0-1631359251920.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2742243%22%20slang%3D%22en-US%22%3ERe%3A%20Percentiles%20from%20frequency%20table%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2742243%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F863782%22%20target%3D%22_blank%22%3E%40bartvana%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20may%20group%20by%20Product%20without%20aggregation%20and%20apply%20former%20procedure%20to%20each%20group%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3Elet%0A%20%20%20%20Source%20%3D%20Excel.CurrentWorkbook()%7B%5BName%3D%22Table1%22%5D%7D%5BContent%5D%2C%0A%20%20%20%20%23%22Grouped%20Rows%22%20%3D%20Table.Group(%0A%20%20%20%20%20%20%20%20Source%2C%20%7B%22Product%22%7D%2C%0A%20%20%20%20%20%20%20%20%7B%7B%22Percentile%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20each%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20allValues%20%3D%20Table.AddColumn(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_%20%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Lists%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20each%20List.Numbers(%5BValue%5D%2C%5BFrequency%5D%2C0)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Percentile%20%3D%20List.Percentile%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20List.Combine(allValues%5BLists%5D)%2C%200.5%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%5BPercentile%5D%0A%20%20%20%20%20%20%20%20%7D%7D%0A%20%20%20%20)%0Ain%0A%20%20%20%20%23%22Grouped%20Rows%22%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3Eor%20use%20function%20instead%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3Elet%0A%20%20%20%20Source%20%3D%20Excel.CurrentWorkbook()%7B%5BName%3D%22Table1%22%5D%7D%5BContent%5D%2C%0A%0A%20%20%20%20fnPercentile%20%3D%20(tbl%20as%20table)%20%3D%26gt%3B%20%0A%20%20%20%20%20%20%20%20let%0A%20%20%20%20%20%20%20%20%20%20%20%20allValues%20%3D%20Table.AddColumn(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20tbl%20%2C%20%22Lists%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20each%20List.Numbers(%5BValue%5D%2C%5BFrequency%5D%2C0)%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20Percentile%20%3D%20List.Percentile%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20List.Combine(allValues%5BLists%5D)%2C%200.5%0A%20%20%20%20%20%20%20%20%20%20%20%20)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20in%0A%20%20%20%20%20%20%20%20%20%20%20%20Percentile%2C%0A%0A%20%20%20%20%23%22Grouped%20Rows%22%20%3D%20Table.Group(%0A%20%20%20%20%20%20%20%20Source%2C%20%7B%22Product%22%7D%2C%0A%20%20%20%20%20%20%20%20%7B%7B%20%22Percentile%22%2C%20each%20fnPercentile(_)%20%7D%7D%0A%20%20%20%20)%0Ain%0A%20%20%20%20%23%22Grouped%20Rows%22%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EHere%20the%20only%20we%20assume%20predefined%20column%20names%20in%20functions.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI'd%20filter%20Frequency%20%3D%200%20before%20grouping.%3C%2FP%3E%0A%3CP%3EIn%20attached%20file%20is%20above%20and%20%22Excel%20variant%22.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Frequent Contributor

Hello,

This is a quite common problem to solve in statistics it seems to me, but I can't find an easy solution in Excel. Am I missing something or is this just something that's not incorporated?

The problem is simple: I have a frequency table, and want to calculate percentiles.

Eg.

Score (0-5) / Number of students

0 / 3

1 / 2

2 / 1

3 / 5

4 / 6

5 / 1

To calculate percentiles, you would line the results up as follows:

0 0 0 1 1 2 3 3 3 3 3 4 4 4 4 4 4 5

You can then use the DAX PERCENTILE function (I understand there is no equivalent in M) on such an "expanded" list.

However:

1) I don't see an easy way to convert the dataset into the "expanded" dataset in M or DAX (in Excel there's this "hack")

2) Even if I would, in real life the expanded dataset would be humungous - and this only to come to a few crunched numbers in the end. In my case, the range of values (left column) goes from 1 to 53 for 10.000 products, and the frequencies (right column) go up to thousands per value, so we're talking about 10.000 lists of 100's of thousands of numbers each.

I found a mathematical explanation here, but it doesn't seem obvious to me to get this done in Excel either...

So I'm hoping there's a formula for this?

5 Replies

@bartvana 

Not sure about the math, but it's easy to repeat Excel sample. For such table

image.png

script which returns median (or 0.5 percentile) is

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    allValues = Table.AddColumn(
        Source, "Lists",
        each List.Numbers([Value],[Frequency],0)),
    Percentile = List.Percentile(            // or List.Median
        List.Combine(allValues[Lists]), 0.5)
in
    Percentile

Performance could be an issue, but that's only to play with actual. Perhaps fixing in memory with Index column or Table.Buffer() could help a bit. Or wrap above with List.Buffer().

 

@Sergei Baklan That's genius!
I'm trying to implement this in my real life data and have only one problem, the grouping by product. Imagine you would have sample data like this and want to calculate the median by product (see also attached workbook where I added the column):

bartvana_0-1631359251920.png

 

best response confirmed by bartvana (Frequent Contributor)
Solution

@bartvana 

You may group by Product without aggregation and apply former procedure to each group

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(
        Source, {"Product"},
        {{"Percentile",
                each
                [
                    allValues = Table.AddColumn(
                    _ ,
                    "Lists",
                    each List.Numbers([Value],[Frequency],0)
                    ),
                    Percentile = List.Percentile (
                        List.Combine(allValues[Lists]), 0.5
                    )                
                ][Percentile]
        }}
    )
in
    #"Grouped Rows"

or use function instead

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

    fnPercentile = (tbl as table) => 
        let
            allValues = Table.AddColumn(
                tbl , "Lists",
                    each List.Numbers([Value],[Frequency],0)
            ),
            Percentile = List.Percentile (
                List.Combine(allValues[Lists]), 0.5
            )                
        in
            Percentile,

    #"Grouped Rows" = Table.Group(
        Source, {"Product"},
        {{ "Percentile", each fnPercentile(_) }}
    )
in
    #"Grouped Rows"

Here the only we assume predefined column names in functions.

 

I'd filter Frequency = 0 before grouping.

In attached file is above and "Excel variant".

@Sergei Baklan You've been of great help, thank you! I managed to do it.

I like to use the Power Query UI as much as possible, and also go in gradual steps so I can understand afterwards what I did, so I ended up with this to make the lists:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"Value", Int64.Type}, {"Frequency", Int64.Type}}),
    //List per Value
    #"Added Custom" = Table.AddColumn(#"Changed Type", "ListPerValue", each List.Numbers([Value], [Frequency], 0), type list),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Frequency"}),
    //List per product (combine lists per value for each product)
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"Product"}, {{"ListPerProduct", each List.Combine([ListPerValue]), type list}}),
    //Show list to check
    #"Extracted Values" = Table.TransformColumns(#"Grouped Rows", {"ListPerProduct", each Text.Combine(List.Transform(_, Text.From), ","), type text})
in
    #"Extracted Values"

First a simple "Add custom column", with the List.Number function.

Then the grouping by doing a generic sum grouping using the UI, then changing the sum function into List.Combine as you showed. The last step is just for checking the result.

Thanks again!

(Workbook attached for future reference).

@bartvana you are welcome, glad to help.