cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Aza33ello
Regular Visitor

Last sales in Calendar Limiter

Hello everybody,

 

I'm trying to make a DAX measure, to find last non empty cell with date limiter. So if there is no sales in current period, the measure should dive into past for 6 month and no longer, and takes the first non blank value.

 

For example, 

Jan 2023 - is a current date

IF Jan 2022 has no sales, it should go to the past between Dec 2021 and Jul 2021.

 

This is my code:

=VAR curYM =
    MIN ( 'Calendar'[YM number]) 
VAR prevY_YMs = 
    CALENDAR(curYM-18; curYM-12)
VAR sales = 
    SUMX (
        VALUES ( ACTSALES3[Product] );
        VAR addSales = 
            ADDCOLUMNS (
                prevY_YMs;
                "@sales";
                    VAR ym = [Date]
                    VAR s =
                        CALCULATE (
                            SUM ( ACTSALES3[Sales TRUB] );
                            ALL ( 'Calendar' );
                            'Calendar'[YM number] = DATE(YEAR(ym); MONTH(ym);1)
                        )
                    RETURN
                        s
            )
        VAR filterYMWithSaleRows = 
            FILTER ( addSales; [@sales] <> 0 )
        VAR lastYMWithSaleRows = 
            MAXX ( filterYMWithSaleRows; [Date] )
        VAR filterLMdata = 
            FILTER ( filterYMWithSaleRows; [Date] = lastYMWithSaleRows )
        VAR result = 
            IF ( NOT ISEMPTY ( filterYMWithSaleRows ); SUMX ( filterLMdata; [@sales] ) )
        RETURN
            result
    )
RETURN
    sales

 

  If I change VAR prevY_YMs =CALENDAR(curYM-18; curYM-12) to GENERATESERIES it will work perfectly. 

 

But the question is, to avoid this function. Any ideas?

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

CALENDAR(curYM-18; curYM-12)

this will not work as each argument of CALENDAR expects a full date, not just a month number.

 

var currD = min ('Calendar'[Date])
var prevY_YMs = CALENDAR(EDATE(currd,-18),EDATE(currD,-12))

 

Note this will create a series of dates, not months.

View solution in original post

2 REPLIES 2
Aza33ello
Regular Visitor

Whoa, it works. Completly forgot about EDATE. Thank you so much, you literally saved my life!!!

lbendlin
Super User
Super User

CALENDAR(curYM-18; curYM-12)

this will not work as each argument of CALENDAR expects a full date, not just a month number.

 

var currD = min ('Calendar'[Date])
var prevY_YMs = CALENDAR(EDATE(currd,-18),EDATE(currD,-12))

 

Note this will create a series of dates, not months.

Helpful resources

Announcements
PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

Top Solution Authors
Top Kudoed Authors