Power Query: Averaging values when greater than zero

RICH937

Board Regular
Joined
Apr 15, 2023
Messages
53
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hi folks.
About 3 weeks ago, I began to try and learn how to use power query, and while I think I'm slowly getting the basics, I cannot for the life of me figure out how to do what would be an insanely easy task in Excel. I have a fairly large database that I'm working with, and am trying to calculate paretos (e.g. 80/20) using average monthly volume for about a dozen or so criteria. My goal is to end with a model that is responsive to monthly changes without the need for refreshing the multiple power pivot workbooks.

I've tried all the usual sources, and I just can't seem to get it right. I've tried unpivoting and grouping, but cannot figure out how to specify the "X>0" requirement for averaging. Tried an insanely long nested if/then, but that didn't work either. Most of what I am finding either (a) has only 1 column that is being averaged, or (b) is "averagingif" using other criteria than values. The latter define those variables using =>. Maybe that is the right way to go, but I can't figure it out.

I'm not sure if the BB will work for this, but if it does, the queries are in the mini sheet below.

One other Q. I have only seen one YT class show adding calculations inot a table loaded from PQ editor, and then reloading the results into editor using the "from table/range" option to get data. I can make that work, but it's going to get huge quick, and it seems to require almost as much refreshing as power pivot. I'm just not sold that is the best way forward.

Any help will be greatly appreciated.

CUST ID
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
ran out of time trying to get the BB to load. hopefully this works.


POWER QUERY HELPER.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAO
1ABRIDGED DATA SET. Actual Line Count: 21,522
2CUST IDCUST PROD IDCHANNELPRODUCTCUSTOMERJan-17Feb-17Mar-17Apr-17May-17Jun-17Jul-17Aug-17Sep-17Oct-17Nov-17Dec-17Jan-18Feb-18Mar-18Apr-18May-18Jun-18Jul-18Aug-18Sep-18Oct-18Nov-18Dec-18Jan-19Feb-19Mar-19Apr-19May-19Jun-19Jul-19Aug-19Sep-19Oct-19Nov-19Dec-19
311Channel 1Product 1Customer 100000000000000000000000009,1752,3502,6002,45014,4503,45001,82523,1501250
422Channel 2Product 2Customer 23,5753,0253,3753,3004,5502,9754,0504,1003,3002,67503756503,8503,5504,3254,8754,0755,1254,3254,1005,9754,4504,6504,9004,3504,3505,0502,8002,8503,6003,1753,2252,8253,8502,650
533Channel 2Product 3Customer 3000000000000000003,5000002,800002,80000000000000
644Channel 3Product 4Customer 40000000000000000000000000002504,50000004,5000
755Channel 2Product 5Customer 500000258008005007505005001,0001,0006251,0005001,0001,0005001,0005001,0001,0001,0001,0005001,0001,0001,00000002,0000
866Channel 1Product 6Customer 60000002,0001,0002,20003,0001,0251,0001,5000065030003501,0004,1501753,0003,07500000000000
977Channel 4Product 7Customer 70000001,7500000000000002,0000000000000000000
1088Channel 5Product 8Customer 82,6101,4002,4102,3002,1102,5601,5002,4001,6306007002,5001,0001,7101,1901,5701,4202,9702,3403,1602,4001,7007006505005005007006,24002,500002,50000
1199Channel 6Product 9Customer 93503904804506005606607206801,1801,0701,3201,0801,3501,1501,8501,5501,1802,0201,7101,1002,4502,2002,9503,0502,8502,5002,2702,8502,2702,6502,6703,3003,2002,9503,700
12510Channel 2Product 10Customer 50000025550250500250400325400700400500250500400400500005009007500005000001,60000
131011Channel 7Product 11Customer 100000000000000010010003601,5101,4001,3501,9501,6501,3501,8001,8001,7501,7501,9501,6002,2101,7201,7202,1001,0600
14312Channel 2Product 12Customer 300000000000000001,8758000000001,90000000000000
151113Channel 5Product 13Customer 11000000000000000000101501902602202002,5209206403,3801,8001,6501,4002,2002,6001,7002,1001,100
161214Channel 8Product 14Customer 121,1751,2501,2759001,2009759751,3001,6001,5251,1501,4502,2759251,3001,0751,1508001,1251,0259251,8501,2501,0251,1501,3001,2501,7506751,2751,5755751,5251,7251,9001,150
17915Channel 6Product 15Customer 92202502902503103303903303606304705304608205508009306801,1709807501,6001,7802,4502,0502,8501,5002,0702,3501,7002,0202,5502,6502,6002,4103,320
181316Channel 6Product 16Customer 130001,25000000000000000000000000000000000
191417Channel 6Product 17Customer 149251,1001,0751,0509751,5001,0508008009007252,4255004251,17587585055042540020000001000000000000
201518Channel 8Product 18Customer 159258256506501,0259758001,1507509251,0008506251,1251,1008501,1501,2001,0251,0001,5001,2009001,4509001,4256751,2751,2501,1007751,3001,2501,3001,500825
211619Channel 1Product 19Customer 1600000000000000000000000000000001,2001,2001,7801,000500
221120Channel 5Product 20Customer 110000000000000000001015013031040008601,0806402,4601,4001,6501,4001,9001,9601,5001,7001,000
231721Channel 6Product 21Customer 1700000000000000000000000000001508751,2501,5005002,5005001,375
241822Channel 6Product 22Customer 189509008758758004001,0751,2258251,0506759259008501,1006001,2001,2501,0259007508751,1251,2005001,3001,5001,0751,4751,0501,5001,0501,3001,2501,000800
251923Channel 9Product 23Customer 193001,3503751,3503001,2501,5501501,525501,6001001,5501001,4001,5004001,0004501,1001,5505501,1005501,4501,0001,45001,4501,7001,75001,75001,7500
262024Channel 7Product 24Customer 201,3257001,3501,3251,3259001,3251,3256501,3251,3001,3007251,3001,3501,3001,3502,0007251,3751,3001,325675725675700725750800800675775750700751,325
272125Channel 10Product 25Customer 210000000000000000000000001,000001,0001,00001,00001,0001,00000
282226Channel 11Product 26Customer 22000000000000000000000000001,000000000000
29727Channel 4Product 27Customer 70000007500000000000001,2500000000000000000
30728Channel 4Product 28Customer 700000000000000000001,0000000000000000000
312329Channel 12Product 29Customer 230000000000000000000001,00000000000000000
322430Channel 8Product 30Customer 2400000000000000000000000000001,0000000000
332431Channel 8Product 31Customer 2400000000000000000000000000001,0000000000
342432Channel 8Product 32Customer 2400000000000000000000000000001,0000000000
352533Channel 8Product 33Customer 25000000000000000000000000000000000001,000
HELPER


POWER QUERY HELPER.xlsx
AQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJBKBLBMBN
2CUST IDCUST+PROD IDCHANNELPRODUCTCUSTOMERTOTAL VOLAVG VOL (need)AVG VOL (wrong)CUST IDCUST PROD IDCHANNELPRODUCTCUSTOMERTOTAL VOLAVG VOLCUST IDCUST PROD IDCHANNELPRODUCTCUSTOMERTOTAL VOLAVG VOL
311Channel 1Product 1Customer 159,5756,6191,65511Channel 1Product 1Customer 159,5756,61911Channel 1Product 1Customer 159,5756,619
422Channel 2Product 2Customer 2128,8753,6823,58022Channel 2Product 2Customer 2128,8753,68222Channel 2Product 2Customer 2128,8753,682
533Channel 2Product 3Customer 39,1003,03325333Channel 2Product 3Customer 39,1003,03333Channel 2Product 3Customer 39,1003,033
644Channel 3Product 4Customer 49,0253,00825144Channel 3Product 4Customer 49,0253,00844Channel 3Product 4Customer 49,0253,008
755Channel 2Product 5Customer 521,50082759755Channel 2Product 5Customer 521,50082755Channel 2Product 5Customer 521,500827
866Channel 1Product 6Customer 624,4251,62867866Channel 1Product 6Customer 624,4251,62866Channel 1Product 6Customer 624,4251,628
977Channel 4Product 7Customer 73,7501,87510477Channel 4Product 7Customer 73,7501,87577Channel 4Product 7Customer 73,7501,875
1088Channel 5Product 8Customer 856,9701,8381,58388Channel 5Product 8Customer 856,9701,83888Channel 5Product 8Customer 856,9701,838
1199Channel 6Product 9Customer 963,3101,7591,75999Channel 6Product 9Customer 963,3101,75999Channel 6Product 9Customer 963,3101,759
12510Channel 2Product 10Customer 510,600505294510Channel 2Product 10Customer 510,600505510Channel 2Product 10Customer 510,600505
131011Channel 7Product 11Customer 1029,2301,4628121011Channel 7Product 11Customer 1029,2301,4621011Channel 7Product 11Customer 1029,2301,462
14312Channel 2Product 12Customer 34,5751,525127312Channel 2Product 12Customer 34,5751,525312Channel 2Product 12Customer 34,5751,525
151113Channel 5Product 13Customer 1123,0401,2806401113Channel 5Product 13Customer 1123,0401,2801113Channel 5Product 13Customer 1123,0401,280
161214Channel 8Product 14Customer 1245,3501,2601,2601214Channel 8Product 14Customer 1245,3501,2601214Channel 8Product 14Customer 1245,3501,260
17915Channel 6Product 15Customer 945,4001,2611,261915Channel 6Product 15Customer 945,4001,261915Channel 6Product 15Customer 945,4001,261
181316Channel 6Product 16Customer 131,2501,250351316Channel 6Product 16Customer 131,2501,2501316Channel 6Product 16Customer 131,2501,250
191417Channel 6Product 17Customer 1418,8258565231417Channel 6Product 17Customer 1418,8258561417Channel 6Product 17Customer 1418,825856
201518Channel 8Product 18Customer 1537,2251,0341,0341518Channel 8Product 18Customer 1537,2251,0341518Channel 8Product 18Customer 1537,2251,034
211619Channel 1Product 19Customer 165,6801,1361581619Channel 1Product 19Customer 165,6801,1361619Channel 1Product 19Customer 165,6801,136
221120Channel 5Product 20Customer 1118,5501,0915151120Channel 5Product 20Customer 1118,5501,0911120Channel 5Product 20Customer 1118,5501,091
231721Channel 6Product 21Customer 178,6501,0812401721Channel 6Product 21Customer 178,6501,0811721Channel 6Product 21Customer 178,6501,081
241822Channel 6Product 22Customer 1836,1501,0041,0041822Channel 6Product 22Customer 1836,1501,0041822Channel 6Product 22Customer 1836,1501,004
251923Channel 9Product 23Customer 1933,4501,0459291923Channel 9Product 23Customer 1933,4501,0451923Channel 9Product 23Customer 1933,4501,045
262024Channel 7Product 24Customer 2037,0501,0291,0292024Channel 7Product 24Customer 2037,0501,0292024Channel 7Product 24Customer 2037,0501,029
272125Channel 10Product 25Customer 216,0001,0001672125Channel 10Product 25Customer 216,0001,0002125Channel 10Product 25Customer 216,0001,000
282226Channel 11Product 26Customer 221,0001,000282226Channel 11Product 26Customer 221,0001,0002226Channel 11Product 26Customer 221,0001,000
29727Channel 4Product 27Customer 72,0001,00056727Channel 4Product 27Customer 72,0001,000727Channel 4Product 27Customer 72,0001,000
30728Channel 4Product 28Customer 71,0001,00028728Channel 4Product 28Customer 71,0001,000728Channel 4Product 28Customer 71,0001,000
312329Channel 12Product 29Customer 231,0001,000282329Channel 12Product 29Customer 231,0001,0002329Channel 12Product 29Customer 231,0001,000
322430Channel 8Product 30Customer 241,0001,000282430Channel 8Product 30Customer 241,0001,0002430Channel 8Product 30Customer 241,0001,000
332431Channel 8Product 31Customer 241,0001,000282431Channel 8Product 31Customer 241,0001,0002431Channel 8Product 31Customer 241,0001,000
342432Channel 8Product 32Customer 241,0001,000282432Channel 8Product 32Customer 241,0001,0002432Channel 8Product 32Customer 241,0001,000
352533Channel 8Product 33Customer 251,0001,000282533Channel 8Product 33Customer 251,0001,0002533Channel 8Product 33Customer 251,0001,000
HELPER
Cell Formulas
RangeFormula
AQ3:AQ35AQ3=Cal_For_Help[@[CUST ID]]
AR3:AR35AR3=Cal_For_Help[@[CUST PROD ID]]
AS3:AS35AS3=Cal_For_Help[@CHANNEL]
AT3:AT35AT3=Cal_For_Help[@PRODUCT]
AU3:AU35AU3=Cal_For_Help[@CUSTOMER]
AV3:AV35AV3=SUM(Cal_For_Help[@[Jan-17]:[Dec-19]])
AW3:AW35AW3=ROUND(AVERAGEIF(Cal_For_Help[@[Jan-17]:[Dec-19]],">0"),0)
AX3:AX35AX3=ROUND(AVERAGE(Cal_For_Help[@[Jan-17]:[Dec-19]]),0)
BF3:BF35BF3=ROUND([@[TOTAL VOL]]/SUM(IF(FILTER(Cal_For_Help[[Jan-17]:[Dec-19]],Cal_For_Help[CUST PROD ID]=[@[CUST PROD ID]])>0,1,0)),0)
 
Upvote 0
Hello RICH937

It is not clear to me what you want. You already have a average calcuation.
Please show a mini sheet with the expected outcome.
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl = Table.AddColumn(Source, "Average Volume", each Number.Round(List.Average(List.RemoveItems(List.Skip(Record.ToList(_),5), {0})))),
    Result = Table.RemoveColumns(tbl, List.Skip(Table.ColumnNames(Source),5))
in
    Result
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl = Table.AddColumn(Source, "Average Volume", each Number.Round(List.Average(List.RemoveItems(List.Skip(Record.ToList(_),5), {0})))),
    Result = Table.RemoveColumns(tbl, List.Skip(Table.ColumnNames(Source),5))
in
    Result
I think this is almost there. What I get though is just an Avg Vol col full of errors. I'm attaching screenshots of both the adv ed in case I entered it wrong, and the result step with error descriptor. Thanks Jeff.
 

Attachments

  • adv editor.PNG
    adv editor.PNG
    17.4 KB · Views: 6
  • result.PNG
    result.PNG
    61.1 KB · Views: 6
Upvote 0
Hello RICH937

It is not clear to me what you want. You already have a average calcuation.
Please show a mini sheet with the expected outcome.
Sorry for the confusion Del. The average calculation is the outcome I am attempting to calculate in Power Query. The values that are there weren't calculated using M. It was done with =ROUND(AVERAGEIF(Cal_For_Help[@[Jan-17]:[Dec-19]],">0"),0)
I can post a minisheet that is blank for the areas I'm looking for if that would help?
 
Upvote 0
Does the code I provided in post #4 work correctly with the sample table you provided in the original post? (because it does for me). If so, the sample table is likely not representative of the actual table in some respect.
 
Upvote 1
Solution
Does the code I provided in post #4 work correctly with the sample table you provided in the original post? (because it does for me). If so, the sample table is likely not representative of the actual table in some respect.
yes. This works perfectly. Thank you. Only thing I had to do was add a reference table to the original table as it promoted "avg vol" to column header, but stripped the "change type" and promoted headers steps. I can't tell you how much this helps.
 
Upvote 0

Forum statistics

Threads
1,215,077
Messages
6,122,991
Members
449,094
Latest member
masterms

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top