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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

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
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Carousel June 2024

Check out the June 2024 Power BI update to learn about new features.

2
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.