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
latheesh2305
New Member

How to get the last non blank value based on the date slicer

Dear Techies,

Let's assume I have a table as shown below. I wanted to get the last valid sales (without blank) for each product and calculate variance for each row.

 

ProductDateSales
A6/1/2022100
A6/15/2022300
A6/25/2022200
A6/28/2022 
A9/13/2022300
A9/28/2022500
A9/30/2022 
A10/5/2022200
A10/12/2022300
A10/17/2022 
B6/1/2022250
B6/15/2022 
B6/25/2022350
B6/28/2022500


Let's say I have a Month-Year slicer and I chose "Jun 2022" then the expected output should be,

ProductDateSalesLast SaleVariance
A6/1/2022100200-100
A6/15/2022300200100
A6/25/20222002000
A6/28/2022 200 
B6/1/2022250500-250
B6/15/2022 500 
B6/25/2022350500-150
B6/28/20225005000


If you notice the above output, for the product A, Last sales was taken from 6/25/2022 although the max date is 6/28/2022 because there was no sales on 6/28/2022. But that is not the case for the product B, where we do have a valid sales for the max date 6/28/2022. 

Let's say I don't select anything on my Month-Year slicer then the expected output should be,

 

ProductDateSalesLast SaleVariance
A6/1/2022100300-200
A6/15/20223003000
A6/25/2022200300-100
A6/28/2022 300 
A9/13/20223003000
A9/28/2022500300200
A9/30/2022 300 
A10/5/2022200300-100
A10/12/20223003000
A10/17/2022 300 
B6/1/2022250500-250
B6/15/2022 500 
B6/25/2022350500-150
B6/28/20225005000

 

Here the last sales for the product is based on the date 10/12/2022 and for the product B it is 6/28/2022.

I'm using Analysis services live connection and I tried various DAX functions but I'm not able to calculate the "Last Sales". Any suggestions or solutions will be of great help.

Thanks,

Latheesh

 

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

Hi, @latheesh2305 

According to your description, you want to display the date of the third week before by a certain date. Right?

Here are the steps you can follow:

(1)This is my test data:

vyueyunzhmsft_0-1662626597444.png

 

(2) We can create two measures : Last Sales  and Variance

Last Sales =
VAR _current_product =
    SELECTEDVALUE ( 'Test'[Product] )
VAR _product_table =
    CALCULATETABLE (
        'Test',
        'Test'[Product] = _current_product,
        ALLSELECTED ( 'Test'[Date] )
    )
VAR _max_date =
    MAXX ( FILTER ( _product_table, [Sales] <> BLANK () ), [Date] )
VAR _max_sales =
    SUMX (
        FILTER (
            ALL ( 'Test' ),
            'Test'[Date] = _max_date
                && 'Test'[Product] = _current_product
        ),
        [Sales]
    )
RETURN
    _max_sales

 

Variance = IF(SUM([Sales])=BLANK(),BLANK(), SUM([Sales]) -[Last Sales])

 

(3)We can put the measures and the fields in the visual , then we can meet your need:

vyueyunzhmsft_1-1662626597452.png

 

vyueyunzhmsft_2-1662626597454.png

 

If this method can't meet your requirement, can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data. We can better understand the problem and help you.

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

View solution in original post

2 REPLIES 2
v-yueyunzh-msft
Community Support
Community Support

Hi, @latheesh2305 

According to your description, you want to display the date of the third week before by a certain date. Right?

Here are the steps you can follow:

(1)This is my test data:

vyueyunzhmsft_0-1662626597444.png

 

(2) We can create two measures : Last Sales  and Variance

Last Sales =
VAR _current_product =
    SELECTEDVALUE ( 'Test'[Product] )
VAR _product_table =
    CALCULATETABLE (
        'Test',
        'Test'[Product] = _current_product,
        ALLSELECTED ( 'Test'[Date] )
    )
VAR _max_date =
    MAXX ( FILTER ( _product_table, [Sales] <> BLANK () ), [Date] )
VAR _max_sales =
    SUMX (
        FILTER (
            ALL ( 'Test' ),
            'Test'[Date] = _max_date
                && 'Test'[Product] = _current_product
        ),
        [Sales]
    )
RETURN
    _max_sales

 

Variance = IF(SUM([Sales])=BLANK(),BLANK(), SUM([Sales]) -[Last Sales])

 

(3)We can put the measures and the fields in the visual , then we can meet your need:

vyueyunzhmsft_1-1662626597452.png

 

vyueyunzhmsft_2-1662626597454.png

 

If this method can't meet your requirement, can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data. We can better understand the problem and help you.

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

amitchandak
Super User
Super User

@latheesh2305 , Use a date table joined with your table

 

this month last value= CALCULATE(lastnonblankvalue(Table[Date], SUM(Table[Sales]) ) ,DATESMTD(ENDOFMONTH('Date'[Date])))

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.

Top Solution Authors