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
061elliven
Frequent Visitor

Coloring percentiles with a stacked column chart

Hi,

 

There are products, that come in weekly, and I want to visualise, how long these products are processed. I want to visualise this with a stacked column chart, where there is amount of products on the y-axis, week number on the x-axis, and bars are colored based on percentiles. Picture of the desired goal can be seen below. For example, >0.8 means, that processing of the products took more time than with 80 % of the cases. 

 

061elliven_0-1660028281856.png

 

Simplified, my data is like

ProductID, ProductArrivalDate, ProductReadyDate, ArrivalYear, ArrivalWeek

Unfortunately, I cannot include productIDs, because they are classified, and thus cannot share the data.

 

Then, I create a new column 'daysTaken', by

daysTaken = DATEDIFF(Table[ProductArrivalDate],Table[ProductReadyDate], DAY).
This seems to work fine, below you can see a screenshot from the table. Data type and format is 'Whole number'.
 
061elliven_1-1660028670981.png

 

However, I have problems with the percentiles. I have tried few approaches:

 

Approach one, counting measure for each percentile, and creating SWITCH column, which classifies products based on these percentiles:

 

First, I created measure for each percentile (following solution of https://community.powerbi.com/t5/Desktop/Percentile-exc-Error/m-p/1927498/highlight/true), below is and example for class '>0' (below 0.2 percentile). Apologies for the formatting, I could not get HTML to work:

 

Measure =
VAR k = 0.2
// Both COUNT and PERCENTILEX.EXC ignore blanks
VAR NumValues =
    COUNT ( Table[daysTaken] )
VAR LowerBound = 1 / ( NumValues + 1 )
VAR UpperBound = NumValues / ( NumValues + 1 )
RETURN
    IF (
        AND ( k >= LowerBound, k <= UpperBound ),
        PERCENTILE.EXC ( Table[daysTaken], k)
    )

 

This also seems to give reasonable results (screenshot below). Again, data formats are 'Whole number'.

 

061elliven_2-1660029780827.png

 

However, when I connect these measures to the products with

 

 

percentiles1 =
SWITCH (
    TRUE (),
    Table[daysTaken]<[Measure], ">0",
    Table[daysTaken]<[Measure 2], ">0.2",
    Table[daysTaken]<[Measure 3], ">0.4",
    Table[daysTaken]<[Measure 4], ">0.6",
    ">0.8"
            )
 
Every row is labeled as '>0.8', as can be seen below.
 
061elliven_6-1660031393261.png

 

If I fix the measures to given values, I get the example picture in the beginning, and everything works fine. For example if I write Table[daysTaken]<25, ">0". However, I have many processes, and thus the percentiles change, so I cannot fix the values.

 

Approach two, count percentile for each product:

 

Next, I tried to count percentile for each product (like solution in https://community.powerbi.com/t5/Desktop/how-to-calculate-percentile-and-values-for-rows-in-a-column...

 

Percentiles =
VAR _ROW1 = CALCULATE(COUNTROWS(Table),Table[daysTaken]<=EARLIER(Table[daysTaken]))
VAR _ROW2 = CALCULATE(COUNTROWS(Table),ALL(Table))
RETURN
_ROW1 /_ROW2

 

But for some reason, it gives '0' for every product. I tried to figure this out, and it seems like for some reason _ROW1 counts only amount of rows, that have the same 'daysTaken' value. If I make it to return only _ROW1, you can see how there are only 4 rows for number 8, which equals amount of 8s in the picture above.

 

061elliven_5-1660031076920.png

So, any idea what am I doing wrong, or how I could easily get the percentiles? Sorry for the long post, but I'm out of potatoes.

1 ACCEPTED SOLUTION
061elliven
Frequent Visitor

If someone in the future is interested, I finally solved this with similar approach as my approach two.

 

First, I created a new column where I counted exact percentiles for each product with

 

exactPercentiles =
VAR _ROW1 = COUNTROWS(FILTER(Table, Table[daysTaken]<=EARLIER(Table[daysTaken]) && Table[process]=EARLIER(Table[process])))
VAR _ROW2 = COUNTROWS(FILTER(Table, Table[process]=EARLIER(Table[process])))
RETURN
_ROW1 / _ROW2
 
Next, I used this exact percentile to classify products to five classes in a new column:
 
percentileClasses =
SWITCH (
    TRUE (),
    Table[exactPercentiles]<0.2, ">0 %",
    Table[exactPercentiles]<0.4, ">20 %",
    Table[exactPercentiles]<0.6, ">40 %",
    Table[exactPercentiles]<0.8, ">60 %",
    ">80 %"
            )
 
FInally, I used this column as a legend in my stacked column chart.

View solution in original post

2 REPLIES 2
061elliven
Frequent Visitor

If someone in the future is interested, I finally solved this with similar approach as my approach two.

 

First, I created a new column where I counted exact percentiles for each product with

 

exactPercentiles =
VAR _ROW1 = COUNTROWS(FILTER(Table, Table[daysTaken]<=EARLIER(Table[daysTaken]) && Table[process]=EARLIER(Table[process])))
VAR _ROW2 = COUNTROWS(FILTER(Table, Table[process]=EARLIER(Table[process])))
RETURN
_ROW1 / _ROW2
 
Next, I used this exact percentile to classify products to five classes in a new column:
 
percentileClasses =
SWITCH (
    TRUE (),
    Table[exactPercentiles]<0.2, ">0 %",
    Table[exactPercentiles]<0.4, ">20 %",
    Table[exactPercentiles]<0.6, ">40 %",
    Table[exactPercentiles]<0.8, ">60 %",
    ">80 %"
            )
 
FInally, I used this column as a legend in my stacked column chart.
061elliven
Frequent Visitor

Here is some test dataset, if it helps

 

ArrivalTimeResultTimeYearArrivalWeekProductID
3.1.2022 11:029.2.2022 15:14202221
3.1.2022 11:049.2.2022 15:28202222
3.1.2022 11:059.2.2022 15:30202223
3.1.2022 11:079.2.2022 15:31202224
25.1.2022 7:0122.2.2022 10:45202255
5.1.2022 13:219.3.2022 8:08202226
25.1.2022 7:0115.2.2022 13:02202257
13.1.2022 11:237.2.2022 14:46202238
13.1.2022 11:197.2.2022 14:48202239
7.12.2021 7:017.2.2022 16:0020215010
7.12.2021 7:017.2.2022 16:0020215011
30.12.2021 7:017.2.2022 12:2320215312
7.12.2021 11:137.2.2022 16:0120215013
10.1.2022 7:4910.2.2022 8:312022314
10.1.2022 7:5021.2.2022 9:432022315
7.1.2022 7:011.3.2022 10:132022216
13.1.2022 11:417.2.2022 14:522022317
10.1.2022 14:518.2.2022 15:112022318
10.12.2021 12:318.2.2022 14:2220215019
25.1.2022 11:538.2.2022 9:012022520
12.1.2022 11:2111.3.2022 11:072022321
12.1.2022 11:2211.3.2022 11:082022322
14.12.2021 15:008.3.2022 12:2220215123
12.1.2022 11:2011.3.2022 11:072022324
14.1.2022 12:3610.2.2022 8:312022325
14.1.2022 15:3515.2.2022 14:482022326
14.1.2022 15:3715.2.2022 14:492022327
14.1.2022 15:3815.2.2022 14:502022328
16.11.2021 14:049.2.2022 13:1620214729
17.1.2022 7:4715.2.2022 13:462022430
17.1.2022 7:4915.2.2022 13:442022431
17.1.2022 7:5015.2.2022 13:432022432
2.2.2022 7:019.3.2022 10:582022633
17.1.2022 15:059.3.2022 10:582022434
18.1.2022 7:5211.2.2022 13:292022435
19.1.2022 8:011.3.2022 12:372022436
19.1.2022 8:0222.2.2022 11:472022437
21.1.2022 7:0123.2.2022 14:472022438
27.1.2022 7:0123.2.2022 15:582022539
24.1.2022 13:1928.2.2022 14:282022540
24.1.2022 13:1317.2.2022 10:392022541
25.1.2022 8:0015.2.2022 13:002022542

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.