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
HoshiWS
Frequent Visitor

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!

 

HoshiWS_1-1663220279580.png

 

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
1 ACCEPTED SOLUTION
ruxandraalina
Helper I
Helper I

Hello, 

 

ruxandraalina_0-1663225027218.png

 

*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)))

View solution in original post

4 REPLIES 4
ruxandraalina
Helper I
Helper I

Hello, 

 

ruxandraalina_0-1663225027218.png

 

*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)))

Hello, thank you for your help!

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

Could you please advise?

 

HoshiWS_1-1663236391199.png

Thank you!

Hello!

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

PowerUserR
Solution Supplier
Solution Supplier

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]
))

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.