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

Subtract a value from another in same column, where other values are equal

I'm hoping for some assistance with data processing in Power BI, either using Power Query or DAX. At this point I am really stuck and can't figure out how to solve this problem.

 

The below table is a list of sales by Product, Month, and Year. The problem with my data is that the value in the sales data is actually cumulative, rather than the raw figure of sales for that month. In other words, the figure is the sum of the number of sales for the month (for that Year and Product combination) and the number of sales for the preceding month. As you will see in the table below, the number gets progressively larger in each category as the year progresses. The true number of sales for TVs in Feb of 2021, for example, is the sales figure of 3 minus the corresponding figure for sales of TVs in Jan of 2021 (1).

I really would appreciate if anyone knows of a solution to this problem. In reality, my table has hundreds of thousands of rows, so I cannot do the calculations manually.

 

Sales Table

Product Sales (YTD) Month Year
TV1Jan2021
Radio4Jan2021
Cooker5Jan2021
TV3Feb2021
Radio5Feb2021
Cooker6Feb2021
TV3Mar2021
Radio6Mar2021
Cooker8Mar2021
TV5Apr2021
Radio7Apr2021
Cooker8Apr2021
TV7May2021
Radio8May2021
Cooker8May2021
TV9Jun2021
Radio10Jun2021
Cooker10Jun2021
TV10Jul2021
Radio10Jul2021
Cooker10Jul2021
TV11Aug2021
Radio13Aug2021
Cooker12Aug2021
TV11Sep2021
Radio13Sep2021
Cooker12Sep2021
TV12Oct2021
Radio14Oct2021
Cooker13Oct2021
TV17Nov2021
Radio19Nov2021
Cooker17Nov2021
TV19Dec2021
Radio20Dec2021
Cooker20Dec2021
TV4Jan2022
Radio2Jan2022
Cooker3Jan2022
TV5Feb2022
Radio3Feb2022
Cooker5Feb2022

 

Is there a way to use Power Query or DAX to create a calculated column with the Raw Sales figure for each month? Something that would check if Product and Year are equal, then subtract the Jan figure from the Feb figure and so on?

Any help will be very much appreciated,

Jim Walker.

 

 

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

JamesFr06_1-1652366265278.png

To insert Monthly sales in your table in a column

View solution in original post

Great, this worked for me, with a minor change as I had to put currentQty - prevQty into CALCULATE() to make it work. I also added in IFERROR in the case that a product had no row for a certain month. 

 

Thanks very much!

 

walker4545_0-1652406872972.png

 

 

The dax code is as follows:

 

Monthly Sales =
VAR currentDate = MONTH('sales new column'[Date])
VAR prevPeriod =
EOMONTH('sales new column'[Date], -1)
VAR currentProduct = 'sales new column'[Product]
VAR currentQty = 'sales new column'[Qty]
VAR prevQty =
IFERROR(LOOKUPVALUE(
'sales new column'[Qty],
'sales new column'[Date], prevPeriod,
'sales new column'[Product], currentProduct
) ,
0)

VAR result =
IF(
MONTH('sales new column'[Date]) = 1,
'sales new column'[Qty],
CALCULATE(currentQty - prevQty)
)
RETURN
result

//DAX Function to calculate raw monthly sales where the default Qty figure is an aggregate of the preceding month's Qty plus new Qty

View solution in original post

6 REPLIES 6
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

JamesFr06_1-1652366265278.png

To insert Monthly sales in your table in a column

Great, this worked for me, with a minor change as I had to put currentQty - prevQty into CALCULATE() to make it work. I also added in IFERROR in the case that a product had no row for a certain month. 

 

Thanks very much!

 

walker4545_0-1652406872972.png

 

 

The dax code is as follows:

 

Monthly Sales =
VAR currentDate = MONTH('sales new column'[Date])
VAR prevPeriod =
EOMONTH('sales new column'[Date], -1)
VAR currentProduct = 'sales new column'[Product]
VAR currentQty = 'sales new column'[Qty]
VAR prevQty =
IFERROR(LOOKUPVALUE(
'sales new column'[Qty],
'sales new column'[Date], prevPeriod,
'sales new column'[Product], currentProduct
) ,
0)

VAR result =
IF(
MONTH('sales new column'[Date]) = 1,
'sales new column'[Qty],
CALCULATE(currentQty - prevQty)
)
RETURN
result

//DAX Function to calculate raw monthly sales where the default Qty figure is an aggregate of the preceding month's Qty plus new Qty
Anonymous
Not applicable

Hi,

 

Excellent have a nice day

rohit_singh
Solution Sage
Solution Sage

Hi @Anonymous ,

You can create something similar to the below based on your requirement : 

rohit_singh_1-1652365897803.png

 

Prev Month =

var _month = max('Cummulative Product Sales'[MonthStart])

Return
CALCULATE(MAX('Cummulative Product Sales'[MonthStart]),
FILTER(ALLSELECTED('Cummulative Product Sales'), 'Cummulative Product Sales'[MonthStart] < _month))

Prev Month Sales =

var _month = max('Cummulative Product Sales'[MonthStart])

var _product = max('Cummulative Product Sales'[Product])

Return
CALCULATE(MAX('Cummulative Product Sales'[Sales (YTD)]),
FILTER(ALLSELECTED('Cummulative Product Sales'), 'Cummulative Product Sales'[Product] = _product && 'Cummulative Product Sales'[MonthStart] < _month))


Curr Month Sales =

var _sales = sum('Cummulative Product Sales'[Sales (YTD)])
var _prev = [Prev Month Sales]

var _curr = _sales - _prev

return
_curr

Basically what we are trying to achieve is to fetch the values of the previous month and previous sales for each row. Once we have these values, we can compute whatever metrics we need.

Kind regards,

Rohit


Please mark this answer as the solution if it resolves your issue.
Appreciate your kudos!  🙂




Anonymous
Not applicable

Hi

 

Do you have a table Date in your model ?

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.