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
Hank_
Helper I
Helper I

Date diff on category: I don't want that

Hello everybody,

I'd like to calculate the difference between 2 specific dates for every product, and then the KPIs based on this date.

I already made it with calculated columns, but I'd like to manage it with DAX (I'm new to DAX).

 

I calculate the date difference:

 

date_diff = DATEDIFF(FIRSTNONBLANK(table[date1],""),FIRSTNONBLANK(table[date2],""),DAY)

 


 and then I calc the KPI as:

 

KPI = IF([date_diff]<30,"OK","KO")

 


This works and my output is:

 

what          days   KPI   date 1 date 2
Product 1   20d   OK   1 jan    20 jan
Product 2   40d   KO   1 jan     9 feb
Product 3   10d   OK   1 feb   20 feb
... ... ...    


The issue arises when I add to my table category above the product. I get:

 

what            days  KPI   date 1 date 2
CATEG. A     51d   KO    1 jan   20 feb
  Product 1   20d   OK    1 jan  20 jan
  Product 2   40d   KO    1 jan    9 feb
  Product 3   10d   OK    1 feb  20 feb
CATEG. B ... ...    
  Product 4 ... ...    


Category gets the date calculated in the same way as products do.

So if computes the difference between the first non-blank dates in all the products of its category. This is not what I'd like to obtain.

 

I'd like to have:


what          days   KPI   date 1 date 2
CATEG. A        
  Product 1   20d   OK   1 jan   20 jan
  Product 2   40d   KO   1 jan     9 feb
  Product 3   10d   OK   1 feb  20 feb


Or, the best would be:

what           days      KPI     date 1 date 2
CATEG. A    23,3d   66,6%   
  Product 1   20d      OK     1 jan   20 jan
  Product 2   40d      KO     1 jan     9 feb
  Product 3   10d      OK     1 feb  20 feb

 

Since I'm a noob with DAX, maybe my approach is incorrect?

 

PS: Sorry for the tables but I cannot post due to invalid HTML... 

1 ACCEPTED SOLUTION
v-eqin-msft
Community Support
Community Support

Hi @Hank_ ,

 

According to my understand, you want to show average of data diff  and the percentage of  "OK" for each Category, right?

You could follow these formulas:

 

1. For date diff

 

DateDiff =
DATEDIFF ( MAX ( 'Table'[Date Start] ), MAX ( 'Table'[Date End] ), DAY )
days =
FORMAT (
    IF (
        HASONEVALUE ( 'Table'[Product] ),
        [DateDiff],
        AVERAGEX ( 'Table', [DateDiff] )
    ),
    "#.0d"
)

 

 

2. For KPI:

 

KPI Value =
IF ( [DateDiff] < 30, "OK", "KO" )
KPI =
VAR _per =
    CALCULATE (
        COUNTROWS ( 'Table' ),
        FILTER ( ALLEXCEPT ( 'Table', 'Table'[Category] ), [KPI Value] = "OK" )
    )
        / CALCULATE ( COUNTROWS ( 'Table' ), ALLEXCEPT ( 'Table', 'Table'[Category] ) )
RETURN
    IF ( HASONEVALUE ( 'Table'[Product] ), [KPI Value], _per )

 

My final output looks like this:

11.27.2.1.PNG

 

Here is the pbix file.

 

Did I answer your question ? Please mark my reply as solution. Thank you very much.
If not, please upload some insensitive data samples and expected output.

 

Best Regards,
Eyelyn Qin

 

View solution in original post

4 REPLIES 4
v-eqin-msft
Community Support
Community Support

Hi @Hank_ ,

 

According to my understand, you want to show average of data diff  and the percentage of  "OK" for each Category, right?

You could follow these formulas:

 

1. For date diff

 

DateDiff =
DATEDIFF ( MAX ( 'Table'[Date Start] ), MAX ( 'Table'[Date End] ), DAY )
days =
FORMAT (
    IF (
        HASONEVALUE ( 'Table'[Product] ),
        [DateDiff],
        AVERAGEX ( 'Table', [DateDiff] )
    ),
    "#.0d"
)

 

 

2. For KPI:

 

KPI Value =
IF ( [DateDiff] < 30, "OK", "KO" )
KPI =
VAR _per =
    CALCULATE (
        COUNTROWS ( 'Table' ),
        FILTER ( ALLEXCEPT ( 'Table', 'Table'[Category] ), [KPI Value] = "OK" )
    )
        / CALCULATE ( COUNTROWS ( 'Table' ), ALLEXCEPT ( 'Table', 'Table'[Category] ) )
RETURN
    IF ( HASONEVALUE ( 'Table'[Product] ), [KPI Value], _per )

 

My final output looks like this:

11.27.2.1.PNG

 

Here is the pbix file.

 

Did I answer your question ? Please mark my reply as solution. Thank you very much.
If not, please upload some insensitive data samples and expected output.

 

Best Regards,
Eyelyn Qin

 

THANK YOU! It's definitely the output I wanted.

 

There's so much stuff to learn for me in this tiny pbix 😃 

Hank_
Helper I
Helper I

Thank you @amitchandak|! Very cool IsInScope, didn't discovered it yet 🙂

 

I this ways I can "hide" values for CATEGORY. If I would calculate for them the actual average days and KPI %? 

 

amitchandak
Super User
Super User

@Hank_ , if what to change the calculation based on the level, you need to use isinscope. But looking at this calculation, I am not able to make, where is a column used, where is measure is used 

https://www.kasperonbi.com/use-isinscope-to-get-the-right-hierarchy-level-in-dax/

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.