Forum Discussion

HoshiWS's avatar
HoshiWS
Frequent Visitor
3 years ago
Solved

Need help with DAX

I need to create a column to calculate the total quantity ordered in the past 365 days, for the same item, same customer.

In excel i can do it with Sumifs (screenshot below), but I am struggling to do the same in Power BI.

 

Can someone please help me with this?

Thanks in advance!

 

 

Order DateCustomerItemQuantityPast year demand
03-Jan-21C001A100
07-Mar-21C001A510
09-May-21C001A3015
03-Apr-22C001A1030
07-Jun-22C001A510
09-Jul-22C001A3015
05-Feb-21C002A200
11-Jul-21C002A5020
05-Jan-22C002A2070
11-Jun-22C002A5070
06-Mar-21C001B300
08-Apr-21C001B2030
06-Feb-22C001B3050
08-Apr-22C001B2030
04-Feb-21C002B150
10-Jun-21C002B4015
04-Jan-22C002B1555
10-May-22C002B4055
  • Hello, 

     

     

    *Sheet1 is your table name

    Past year demand DAX = 
    var ctx_max_date = max(Sheet1[Order Date])
    var ctx_prev_date = date(year(ctx_max_date), month(ctx_max_date), day(ctx_max_date)-365)
    return
    if(
        isblank(CALCULATE(sum(Sheet1[Quantity]), filter(ALLEXCEPT(Sheet1, Sheet1[Customer], Sheet1[Item]), Sheet1[Order Date] < ctx_max_date && Sheet1[Order Date] >= ctx_prev_date))), 0, 
        CALCULATE(sum(Sheet1[Quantity]), filter(ALLEXCEPT(Sheet1, Sheet1[Customer], Sheet1[Item]), Sheet1[Order Date] < ctx_max_date && Sheet1[Order Date] >= ctx_prev_date)))

4 Replies

  • Hello, 

     

     

    *Sheet1 is your table name

    Past year demand DAX = 
    var ctx_max_date = max(Sheet1[Order Date])
    var ctx_prev_date = date(year(ctx_max_date), month(ctx_max_date), day(ctx_max_date)-365)
    return
    if(
        isblank(CALCULATE(sum(Sheet1[Quantity]), filter(ALLEXCEPT(Sheet1, Sheet1[Customer], Sheet1[Item]), Sheet1[Order Date] < ctx_max_date && Sheet1[Order Date] >= ctx_prev_date))), 0, 
        CALCULATE(sum(Sheet1[Quantity]), filter(ALLEXCEPT(Sheet1, Sheet1[Customer], Sheet1[Item]), Sheet1[Order Date] < ctx_max_date && Sheet1[Order Date] >= ctx_prev_date)))
    • HoshiWS's avatar
      HoshiWS
      Frequent Visitor

      Hello, thank you for your help!

      I used the same DAX formula but got the following results instead.

      Could you please advise?

       

      Thank you!

      • ruxandraalina's avatar
        ruxandraalina
        Helper I

        Hello!

        You need to create a measure, not a column. Sorry, I forgot to mention this. 

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi,

    try this: 

    PYD =
    CALCULATE (
        SUM ( 'Table (3)'[Quantity] ),
        ALL ( 'Table (3)' ),
        'Table (3)'[Order Date] < EARLIER('Table (3)'[Order Date])&&
        'Table (3)'[Customer] = EARLIER ( 'Table (3)'[Customer] ) &&
        'Table (3)'[Item] = EARLIER ( 'Table (3)'[Item]
    ))