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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

How to convert SQL windowed function to DAX

I have this table:

NormalisedDayNoCustomerDate
0A02/09/2018
24A26/09/2018
0B21/09/2018
4B25/09/2018
0C17/09/2018
8C25/09/2018
9C26/09/2018
9C26/09/2018
0D09/09/2018

 

In SQL worked out the NDayNo. by doing following calc

COALESCE(DATEDIFF(day,FIRST_VALUE(Date) OVER (PARTITION BY Customer ORDER By Date),Date),0)

NDayNo. = So Im working out the first time the customer contacts in the month (=0) and then how many days until next time they contact.

 

How do I calculate this as a DAX query?

1 ACCEPTED SOLUTION

You can create the below calculated column:

NDayNo. =
VAR CurrentCustomer = 'Table'[Customer]
VAR CurrentDate = 'Table'[Date]
VAR FirstDayofCustomer =
    CALCULATE (
        MIN ( 'Table'[Date] ),
        'Table'[Customer] = CurrentCustomer,
        ALL ( 'Table' )
    )
RETURN
    DATEDIFF ( FirstDayofCustomer, CurrentDate, DAY )

View solution in original post

3 REPLIES 3
AkhilAshok
Solution Sage
Solution Sage

If you just want a measure, you could do somethng like this:

 

NDayNo = 
VAR CurrentCustomer =
    MAX ( 'Table'[Customer] )
VAR CurrentDate =
    MAX ( 'Table'[Date] )
VAR FirstDayofCustomer =
    CALCULATE (
        MIN ( 'Table'[Date] ),
        'Table'[Customer] = CurrentCustomer,
        ALL ( 'Table')
    )
RETURN
    DATEDIFF ( FirstDayofCustomer, CurrentDate, DAY )
Anonymous
Not applicable

Thanks, Is there anyway to calculate this as a calc column?

 

Reason being I need to store the value at row level, and for it to dynamically change based on filters applied.

You can create the below calculated column:

NDayNo. =
VAR CurrentCustomer = 'Table'[Customer]
VAR CurrentDate = 'Table'[Date]
VAR FirstDayofCustomer =
    CALCULATE (
        MIN ( 'Table'[Date] ),
        'Table'[Customer] = CurrentCustomer,
        ALL ( 'Table' )
    )
RETURN
    DATEDIFF ( FirstDayofCustomer, CurrentDate, DAY )

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.