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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

March2025 Carousel

Fabric Community Update - March 2025

Find out what's new and trending in the Fabric community.