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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
DaGemsta
Helper I
Helper I

Finding Average Item rate while grouping by Item code for a date range (slicer)

Hi guys

 

Scratching my head with this one.

 

We have the odd situation where agents will enter dummy values for the Item Rate "£0.00" and "£2.22" on sales orders.

 

After the sales are signed off, we then have to go into the orders and correct these dummy orders by working out the average Item Rate per Item code from all the other sales orders, and then we use this as the Item Rate instead of the dummy values.

 

For example see same data below

 

Document NumberDateItemItem RateOriginally Ordered QtyScheduled Sell
SO0235961

25/04/2022

2£0.81500£405.75
SO0235993

25/04/2022

2£0.85500£426.10
SO023599425/04/20222£0.85500£426.10
SO023599525/04/20222£0.00500£0.00
SO023617924/04/20222£0.96600£577.74
SO023454226/04/20223£0.00553£0.00
SO024006426/04/20223£1.451747£2,532.28
SO023399624/04/20223£1.454053£5,876.85
SO023413921/04/20223£1.451686£2,444.70
SO023639426/04/20223£2.221021£2,266.62
SO023644821/04/20223£2.22243£539.46
SO024102524/04/20223£0.00921£0.00
SO024141124/04/20223£2.22463£1,027.86
SO024142821/04/20223£0.00782£0.00
SO024143521/04/20223£2.221350£2,997.00
SO023697526/04/20225£0.653716£2,415.40
SO023697626/04/20225£0.001372£0.00
SO023697721/04/20225£0.652999£1,949.35
SO023698626/04/20225£0.651600£1,040.00
SO023698721/04/20225£0.001998£0.00
SO023698821/04/20225£0.653499£2,274.35
SO023701024/04/20225£0.652025£1,316.25
SO024044124/04/20225£0.96474£455.66
SO024044721/04/20225£0.963367£3,236.70
SO024278821/04/20225£2.22500£1,110.00
SO024279021/04/20225£2.221252£2,779.44

 

So for Item code 3 we first remove all rows where the Item Rate is £0.00 or £2.22 leaving the following:

 

Document NumberDateItemItem RateOriginally Ordered QtyScheduled Sell
SO024006422/04/20223£1.451747£2,532.28
SO023399624/04/20223£1.454053£5,876.85
SO023413921/04/20223£1.451686£2,444.70

 

We then total the Originally Ordered Qty and Delivered Sell for the remaining orders

 

Originally Ordered Qty Total = 7486

Scheduled Sell Total = £10,853.83

 

Then divide the Scheduled Sell Total / Originally Ordered Qty Total to get the Average Item Rate = £1.45

 

You then have to put the removed lines back in and add a column with the Average Item Rate you've just calculated.

 

Now using the new Average Item Rate I can calculate the new "Average Scheduled Sell" price.

 

Document NumberDateItemItem RateOriginally Ordered QtyScheduled SellAverage Item RateAverage Scheduled Sell
SO023454221/04/20223£0.00553£0.00

£1.45

£801.85

SO024006422/04/20223£1.451747£2,532.28£1.45£2533.15
SO023399624/04/20223£1.454053£5,876.85£1.45£5876.85
SO023413921/04/20223£1.451686£2,444.70£1.45£2444.70
SO023639422/04/20223£2.221021£2,266.62£1.45£1480.45
SO023644824/04/20223£2.22243£539.46£1.45£352.35
SO024102524/04/20223£0.00921£0.00£1.45£1335.45
SO024141121/04/20223£2.22463£1,027.86£1.45£671.35
SO024142822/04/20223£0.00782£0.00£1.45£1133.90
SO024143524/04/20223£2.221350£2,997.00£1.45£1957.50

 

This all needs to be grouped by the Item code and affected by the Date range on the page slicer.

 

I've only managed to get part way there by duplicating the orders table and manipulating it in power query but this is incredibly slow and is not affected by the date as it takes the average across all orders.

 

Anyone brave enough to have a go? 🙂

2 ACCEPTED SOLUTIONS
speedramps
Super User
Super User

 

Hi Da Geemta

 

I think this is what you are asking for.

Create a table visual with item number and then the Ave rate.

Please click thumbs up and Accept As Solution because I was "brave enough" to have a go 😀

 
Ave rate =
// get the current visual row values
VAR myrate = SELECTEDVALUE('Table'[Item Rate])
VAR myitem = SELECTEDVALUE('Table'[Item])

// create a subset for the item. excluding £0.00 and £2.22 obviously so they dont screw up the avergaes
VAR mysubset =
FILTER(ALL('Table'),
'Table'[Item] = myitem &&
NOT ('Table'[Item Rate]) IN {0, 2.22}
)
RETURN

// only get the average for £0.00 and £2.22 rows, othwerwise return the original value

IF(myrate IN {0,2.22},

CALCULATE(
AVERAGE('Table'[Item Rate]),
mysubset
),

myrate
)

View solution in original post

speedramps
Super User
Super User

Hi again DaGemsta

 

Firstly you are being a bit cheeky adding a new problem to the ticket. 😀

Please raise 1 problem per ticket. If you need to change or expand the ticket because you did not ask the right question, them accept the solution and raise a new ticket and give the solver twice the kudos scores for doing twice the work. Thank you. 👍

 

Secondly, you have now mention a "Received" field which is not is you example data ! 🤔

 

May I suggest that you never use / in Power BI.
Always use DIVIDE to capature any divsion by zero errors. 😇

 

I cant answer you exact questioin because you hace not provided the  "Received" field, but I am pretty sure this info will help.

 

Look at these 2 measures carefully:-

Sum value =  SUM('Table'[Originally Ordered Qty]) * [Ave rate]

Sumx value = SUMX('Table','Table'[Originally Ordered Qty] * [Ave rate])

 

Both will return the same answer for each a row in table visual with

Document number, Date, Item, Item Rate, Ave Rate, Sum value, Sumxvalue.

 

However, only the Sumx value will return the correct sub totals and grand totals.

 

This is because SUMX is an iterator.  see https://docs.microsoft.com/en-us/dax/sumx-function-dax

 

Click here to download my example 

 

Now please smash those thumbs up and Accept As Solution buttons to give kudos for helping you. Thank you! 😎

 

 

 

View solution in original post

6 REPLIES 6
speedramps
Super User
Super User

Hi again DaGemsta

 

Firstly you are being a bit cheeky adding a new problem to the ticket. 😀

Please raise 1 problem per ticket. If you need to change or expand the ticket because you did not ask the right question, them accept the solution and raise a new ticket and give the solver twice the kudos scores for doing twice the work. Thank you. 👍

 

Secondly, you have now mention a "Received" field which is not is you example data ! 🤔

 

May I suggest that you never use / in Power BI.
Always use DIVIDE to capature any divsion by zero errors. 😇

 

I cant answer you exact questioin because you hace not provided the  "Received" field, but I am pretty sure this info will help.

 

Look at these 2 measures carefully:-

Sum value =  SUM('Table'[Originally Ordered Qty]) * [Ave rate]

Sumx value = SUMX('Table','Table'[Originally Ordered Qty] * [Ave rate])

 

Both will return the same answer for each a row in table visual with

Document number, Date, Item, Item Rate, Ave Rate, Sum value, Sumxvalue.

 

However, only the Sumx value will return the correct sub totals and grand totals.

 

This is because SUMX is an iterator.  see https://docs.microsoft.com/en-us/dax/sumx-function-dax

 

Click here to download my example 

 

Now please smash those thumbs up and Accept As Solution buttons to give kudos for helping you. Thank you! 😎

 

 

 

Awesome Source!!! Thanks Speedy 😁

speedramps
Super User
Super User

Hi again DeGemsta

 

I am on the mobile phone at the moment and will reply properly when I get chance on the laptop.

 

It sounds like you may need to use the SUMX iterator.

 

SUMX is a classic method for getting grand totals from multiple rows with value = unit price x quantity.

 

Obviously you cant SUM unit price x quantity. That wont make sense.

 

 

 

Thanks! Looking forward to it 🙂

speedramps
Super User
Super User

 

Hi Da Geemta

 

I think this is what you are asking for.

Create a table visual with item number and then the Ave rate.

Please click thumbs up and Accept As Solution because I was "brave enough" to have a go 😀

 
Ave rate =
// get the current visual row values
VAR myrate = SELECTEDVALUE('Table'[Item Rate])
VAR myitem = SELECTEDVALUE('Table'[Item])

// create a subset for the item. excluding £0.00 and £2.22 obviously so they dont screw up the avergaes
VAR mysubset =
FILTER(ALL('Table'),
'Table'[Item] = myitem &&
NOT ('Table'[Item Rate]) IN {0, 2.22}
)
RETURN

// only get the average for £0.00 and £2.22 rows, othwerwise return the original value

IF(myrate IN {0,2.22},

CALCULATE(
AVERAGE('Table'[Item Rate]),
mysubset
),

myrate
)

lol Thanks speedramps 🙂

 

I've added a measure to work out delivered sell using the ave rate you generated

 

Ave Delivered Sell = CALCULATE(SUM('Saved Search'[Quantity Fulfilled/Received])*[Ave Rate])


The above works great on the one visual but I can't total it and I can't then use it to calculate the net profit etc for the report.

 
DaGemsta_0-1651738070739.png

 

Am I doing something wrong?

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 MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.