Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
andigaruda
Frequent Visitor

DAX formula to calculate Median and Percentile Q1 and Q3 from multiple column

Hello Experts,
I need your expertise here.

I am new in DAX formula and I am working ini some data where I need to calculate Median and Percentile Q1 and Q3 with mutiple colums as a source of data . In this case Data1 Data 2 Data 3
I have tried to use median and median x formula ; but still fail 
Thanks for the help.

Table data as follow : 

 

MonthWeekDateData1Data 2Data 3
Sep321/09/20191,391,361,39
Sep321/09/20191,381,321,38
Sep321/09/20191,391,341,39
Sep322/09/20191,391,341,39
Sep423/09/20191,31,341,3
Sep424/09/20191,41,381,4
Sep425/09/20191,361,391,36
Sep426/09/20191,341,381,34
Sep427/09/20191,321,371,32
Sep428/09/20191,321,391,32
Sep429/09/20191,31,371,3
Sep530/09/20191,391,311,39
Oct101/10/20191,31,351,3
Oct102/10/20191,381,331,38
Oct103/10/20191,391,381,39
Oct104/10/20191,351,41,35
Oct105/10/20191,361,311,36
1 ACCEPTED SOLUTION

I can see how that structure will be helpfull while gathering the data, but I can't see the reason why you should not reorder the data in Power BI. With reordered data, you can still achieve a visualisation correpsponding to the input by using the matrix visual, with dataset name as column header. And at the same time, it will make your DAX code much less complicated.

View solution in original post

6 REPLIES 6
sturlaws
Resident Rockstar
Resident Rockstar

Hi @andigaruda ,

is it a requirement to keep the table with that structure? I ask because if you use the "Unpivot Columns"-functionality of Power Query/Edit Queries, you will have one column which identifies which data set it is, and one column with all the values. And since there is one column with all the values, calculating e.g. the median will become trivial

DAX formula to calculate Median and Percentile Q1 and Q3 from multiple column.PNG

 

Cheers,
Sturla


If this post helps, then please consider Accepting it as the solution. Kudos are nice too.

Dear Sturlaws,

Thank you for your response and propose solution,  Unfortunately the table is required using that structure .

That is giving me a headache . 🙂

Best regards

@andigaruda,

I got this to work for your sample dataset:

 

median =
VAR allDatasets =
    UNION (
        SELECTCOLUMNS ( 'Table'; "Data"; 'Table'[Data1] );
        SELECTCOLUMNS ( 'Table'; "T2"; 'Table'[Data 2] );
        SELECTCOLUMNS ( 'Table'; "T3"; 'Table'[Data 3] )
    )
VAR addBreakTies =
    ADDCOLUMNS (
        allDatasets;
        "Data+rand"; [Data]
            + RANDBETWEEN ( 1; 1000 ) / 1000000
    )
VAR addRank =
    ADDCOLUMNS (
        addBreakTies;
        "Rank"; RANKX ( addBreakTies; [Data+rand];;; DENSE );
        "NumberOfValues"; CALCULATE ( COUNTROWS ( allDatasets ); ALL ( 'Table' ) );
        "midPoint";
        VAR nov =
            CALCULATE ( COUNTROWS ( allDatasets ); ALL ( 'Table' ) )
        RETURN
            IF ( MOD ( nov; 2 ) = 0; DIVIDE ( nov; 2 ); DIVIDE ( nov - 1; 2 ) )
    )
VAR _median =
    SUMMARIZE ( FILTER ( addRank; [Rank] = [midPoint] ); [Data] )
RETURN
    _median

 

But will depend on how you want your report to look like. If you can provide a mockup of how you want your report to look like(e.g. in excel), I can advise a more spesific solution

Out of curiosity, why do you have to keep the table in this structure?

Cheers,
Sturla

Dear Sturla, 
Thank you for your help.
The goal is to create a box and whisker plot over the time ( Monthly ; Weekly & Daily ) to  show the shape of the distribution, its central value, and its variability to the operator level as a process owner as shown below .

Table structure is derived form the actual form used by operator to record the data,  
With this structure . we hope that operator easily identify the abnormal value of range between Data 1 Data 2 and Data 3

 
 
 
 
 

 

 
 

Best Regard

 

 

image.png

 

I can see how that structure will be helpfull while gathering the data, but I can't see the reason why you should not reorder the data in Power BI. With reordered data, you can still achieve a visualisation correpsponding to the input by using the matrix visual, with dataset name as column header. And at the same time, it will make your DAX code much less complicated.

Dear Sturla
Thank you for your help and suggestion 
Have a nice day  

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.