Average Last 3 Customer Sales: Power Query, DAX Measure or Worksheet Formula: Excel Magic Trick 1552

Поделиться
HTML-код
  • Опубликовано: 5 фев 2025
  • Download Excel Finished Files: excelisfun.net...
    Entire page with all Excel Files for All Videos: excelisfun.net...
    In this video learn how to Average a customers last three sales (last three dates) using Excel Worksheet Formulas, Power Query M Code and a Power Pivot & Power BI DAX Measure. Learn about the Table.Sort Power Query M Code Function and how there is a bug that prevents the next step from updating when source data changes.
    Topics:
    1. (00:07) Introduction
    2. (00:50) Sort Method
    3. (02:09) Excel Worksheet Formulas, including AGGREGATE and AVERAGEIFS functions.
    4. (05:27) LARGE Function and New Excel Office 365 Calculation Engine
    5. (08:15) Power Query M Code, including Table.Sort, List.Average and List.FirstN M Code functions
    6. (12:46) Power Query Parameter Query with condition coming from Excel Worksheet
    7. (14:05) Power Pivot & Power BI DAX Measure, including TOPN, CALCULATE and AVERAGE DAX Functions
    8. (17:08) DAX Parameter from Excel Worksheet using VALUES DAX Function.
    9. (18:43) Add new records to table and test formulas
    10. (19:00) Fix Power Query Table.Sort bug using Dummy Insert Column
    11. (20:08) Fix Power Query Table.Sort bug using Table.Sort function inside Table.Group Function
    12. (22:12) Conclusion

Комментарии • 128

  • @GeertDelmulle
    @GeertDelmulle 5 лет назад +5

    Great video, subject and format - and action packed as always.
    For me these comparison videos take the cake: one specific example and 3 ways to do it: just great!
    Only very few people do this and none are as popular as you, Mike! Cudos!
    BTW: I’d prefer it when you import the variable/parameter into the right m-step without requiring an additional query, you know, like you’ve done before. I actually literally showed that to some colleagues at work today (specifically: the keyword trick I mentioned earlier). Now, they have seen me use and explain PQ before, but they were flabbergasted when I constructed and “stole” the right formula and pasted it in the right place. Furthermore, coup de theatre: their jaws dropped on the floor when I removed the redundant query (I just added) altogether and it still worked... Hahaha, that was funny! You should have seen the look on their faces... :-)

    • @GeertDelmulle
      @GeertDelmulle 5 лет назад +2

      Mike, here’s a little request: can you do an exercise where you compute a cumulative column in DAX?
      You get to use the following ingredients: index column, rankx, and topN. That should do the trick...
      Furthermore, I wonder how to do that in PQ... Do you accept the challenge?
      As you know: performing calculations across the rows/records is quite the challenge in PQ and DAX...

    • @excelisfun
      @excelisfun  5 лет назад +1

      The look on their faces say: "Geert is awesome!!!!" : )
      BTW, I do not know what you mean when you wrote: "import the variable/parameter into the right m-step without requiring an additional query"... What is the process?, what video did I show it in...?

    • @excelisfun
      @excelisfun  5 лет назад +1

      @@GeertDelmulle , I have so many demands right now bearing down on me. I have seen both Power Query and DAX solutions... I thought I did a Power Query type running total... But now I do not remember. I just added your cool Running Total in DAX and Power Query to my list on my desk : ) But not sure when I will get to it...

    • @excelisfun
      @excelisfun  5 лет назад +4

      ​@@GeertDelmulle , O Geert, you did it to me again... You ask a cool question and I can not resist... Below is the data set, Worksheet Formulas, DAX Formulas and DAX Formulas for running totals by Month in a column:
      Data Set:
      Date Units
      5/29/19 36
      5/30/19 64
      5/31/19 42
      6/1/19 76
      6/2/19 77
      6/3/19 80
      6/4/19 12
      6/5/19 39
      6/6/19 75
      6/7/19 82
      6/8/19 59
      6/9/19 56
      6/10/19 37
      6/11/19 55
      [1] Worksheet Formula Cumulative Totals: =SUM(C$5:C5)
      [2] Worksheet Formula Cumulative Totals by Month: =SUMPRODUCT(--(MONTH(B$5:B5)=MONTH(B5)),C$5:C5)
      [3] DAX Calculated Column Cumulative Totals:
      =CALCULATE(SUM(fUnits[Units]),FILTER(fUnits,fUnits[Date]

    • @GeertDelmulle
      @GeertDelmulle 5 лет назад

      Mike, about: inserting a variable/parameter w/o additional query:
      The process is this:
      - create a one-cell table in Excel and load it into PQ - let’s call this the parameter query;
      - in that new query drill down to the scalar value of the parameter;
      - copy the row and column refs and paste them in the first (=source) step of said query;
      - delete all other steps in that query;
      - copy the formula from the source step;
      - paste it to where-ever you need it (another step in another query) - e.g. as the value in a filter step, to make the latter variable.
      - finally: delete the now redundant parameter query.
      Done.
      BTW: it was in that video where you avoided a Firewall Error Message in exactly this way!...
      PS: it is quicker to do it than to describe it here in the comments. LOL! :-)

  • @LeilaGharani
    @LeilaGharani 5 лет назад +2

    Love the different solutions. Thank you Mike!

    • @excelisfun
      @excelisfun  5 лет назад

      You are welcome, Teammate!!! Just too many ways to have fun in Excel : )

  • @10ozGold
    @10ozGold 5 лет назад +1

    Excellent video Mike! Love formula vs M code vs DAX comparisons. Please make more videos like these. My mind is blown.

    • @excelisfun
      @excelisfun  5 лет назад

      Many more to come! Thanks for your support with your comments, Thumbs Up and Sub : )

  • @entertainmentgalaxy971
    @entertainmentgalaxy971 5 лет назад +4

    Amazing. Power Query is magic. thanks for sharing all three ways in a single video. Love your new videos to handle a single problem with three different solutions. GBU.
    Thanks for sharing

    • @excelisfun
      @excelisfun  5 лет назад

      You are welcome for the share and three way fun with Power Query Worksheet Formulas and DAX!!!

  • @wynhopkins4023
    @wynhopkins4023 5 лет назад +1

    So much good stuff covered there, and good work flagging the insert index sort bug fix

    • @excelisfun
      @excelisfun  5 лет назад

      So much good stuff and fun = that is because Excel is still the tool that offers it all: Worksheet, Power Query, Data Model, DAX!!! All except those delicious interactive visualizations and monthly updates like in Power BI : ) Glad you had fun with this good stuff, Teammate Wyn!!! : )

  • @katerina6495
    @katerina6495 5 лет назад +2

    Thank you for the great video Mike, it is always great pleasure to watch, so informative. Have a great day Mike

    • @excelisfun
      @excelisfun  5 лет назад

      You are welcome, Katerina!!!! Thanks for the good day wishes : )

  • @vida1719
    @vida1719 5 лет назад +2

    Nice comparison and good to know how to fix the bug

    • @excelisfun
      @excelisfun  5 лет назад

      Yes indeed, comparisons and fixes are fun and important : )

  • @artcheezz37
    @artcheezz37 5 лет назад +2

    Thanks a lot. This video is very useful. 👍

    • @excelisfun
      @excelisfun  5 лет назад

      You are welcome a lot! Thanks for your support!

  • @excelbear6860
    @excelbear6860 5 лет назад +2

    love to see different solutions from formulas, Power Query, and Power Pivot!

  • @Victor-ol1lo
    @Victor-ol1lo 5 лет назад +2

    Thanks for the awsome video Mike ! Great to see the different PT´s in action ! Thanks and as always thumbs up !!!!

    • @excelisfun
      @excelisfun  5 лет назад

      Thanks for the always thumbs ups, Victor! What do you mean "PT's"?

  • @nimrodzik1
    @nimrodzik1 5 лет назад +2

    Thank You Mike. Thumbs Up :)

    • @excelisfun
      @excelisfun  5 лет назад

      You are welcome, nimrodzik1!!!!!

  • @rafaelmonteiro4332
    @rafaelmonteiro4332 4 года назад

    Awesome video. Helped me a lot as always. I would be stuck forever in that Query bug.

  • @rrrprogram8667
    @rrrprogram8667 5 лет назад +2

    Good one mike... Its been a whileeeee since i have used aggregate function... Its all power query and dax now....
    This video revised old memories... And i love it... Thanks for another awesome video

    • @excelisfun
      @excelisfun  5 лет назад

      You are welcome! These are fun when we do it with Worksheet, DAX and M Code Formulas! What is it that you are doing where you are using Power Query and DAX all the time?

    • @rrrprogram8667
      @rrrprogram8667 5 лет назад +1

      @@excelisfun i am automating most of the redundant tasks with power query and then summarising it using dax... And it saves tonnes of time... And using saved time it to have fun with R language

    • @excelisfun
      @excelisfun  5 лет назад

      @@rrrprogram8667 RRRRRRRRRRR is fun : )

    • @rrrprogram8667
      @rrrprogram8667 5 лет назад

      @@excelisfun lol...

  • @avilammon
    @avilammon 5 лет назад +2

    Thank you ... I exactly stuck in that Power query bug.. and you enlighten me.
    Thank you so much

    • @excelisfun
      @excelisfun  5 лет назад

      You are welcome! I am glad that this video came out now to help with your current Table.Sort issue. Thanks for your support with your comment, Thumbs Up and your Sub : )

  • @shoeshines2121
    @shoeshines2121 4 года назад

    Great video showcasing the different solutions.

  • @SyedMuzammilMahasanShahi
    @SyedMuzammilMahasanShahi 5 лет назад +2

    Thanks Mike for this EXCELlent video.

    • @excelisfun
      @excelisfun  5 лет назад

      You are welcome for the EXCELlent video, Syed : )

  • @chrism9037
    @chrism9037 5 лет назад +2

    This was great Mike, thanks!

    • @excelisfun
      @excelisfun  5 лет назад +1

      You are welcome most awesome Excel Online Teammate Chris!!!!

  • @NoShadowOfDoubt1
    @NoShadowOfDoubt1 5 лет назад

    How did I miss this great video!

    • @excelisfun
      @excelisfun  5 лет назад +1

      I do not know? But I am glad that you found it. It definitely is an action packed video with lots of advanced tips : )

  • @enriquedominguez9709
    @enriquedominguez9709 5 лет назад

    One problem, three ways to solve. Great idea! complete explanation. thank you Mike.

    • @excelisfun
      @excelisfun  5 лет назад

      Glad you like it, enrique!

  • @athandapani
    @athandapani 5 лет назад +2

    This was awesome video, thanks for sharing 👍

    • @excelisfun
      @excelisfun  5 лет назад

      You are welcome for the share! Thanks for your support : )

  • @johnborg6005
    @johnborg6005 5 лет назад +1

    Beautiful staff!!! Thanks mike.

    • @excelisfun
      @excelisfun  5 лет назад

      You are welcome, John B. : )

  • @davebowman5392
    @davebowman5392 5 лет назад +4

    Thanks Mike!

    • @excelisfun
      @excelisfun  5 лет назад

      You are welcome, Dave!!!!

    • @excelisfun
      @excelisfun  5 лет назад

      Which method do you like?

    • @davebowman5392
      @davebowman5392 5 лет назад

      @@excelisfun I've had almost the same calculation to work on this week, I've had to use a sheet formula, but I like the DAX formulas, probably due to writing so many Access queries

    • @excelisfun
      @excelisfun  5 лет назад

      @@davebowman5392 Cool : )

  • @kellyray1388
    @kellyray1388 5 лет назад +3

    Love the theme music

    • @excelisfun
      @excelisfun  5 лет назад

      Nice to have music while doing Excel : )

  • @Luciano_mp
    @Luciano_mp 5 лет назад +2

    Amazing, thanks Mike.

    • @excelisfun
      @excelisfun  5 лет назад

      Glad it is amazing for you, Luciano!!!

  • @nadyelnahal
    @nadyelnahal 5 лет назад +4

    Perfect

    • @excelisfun
      @excelisfun  5 лет назад

      Glad it is perfect for you!

  • @MalinaC
    @MalinaC 5 лет назад +2

    Thumbs up, of course! Agregate, PQ PP - brilliant!

    • @excelisfun
      @excelisfun  5 лет назад

      Glad you liked the video, Malina : )

  • @alirezamogharabi8733
    @alirezamogharabi8733 5 лет назад +2

    Very helpful, thanks.

    • @excelisfun
      @excelisfun  5 лет назад +1

      You are welcome for the helpful fun, ali!!

  • @reng7777
    @reng7777 4 года назад

    Your just... A GENIUS!!!!

  • @ismailismaili0071
    @ismailismaili0071 5 лет назад

    thank you so so much I swear was looking for this formula

    • @excelisfun
      @excelisfun  5 лет назад

      You are welcome, Ismail!!!

  • @sherryizzie5309
    @sherryizzie5309 5 лет назад +1

    Hi Mike, Great tips! , Another way to workaround the table sort bugs is to buffer the sorting table: = Table.Buffer(Table.Sort(#"Changed Type",{{"Customer", Order.Ascending}, {"DateTime", Order.Descending}}))

    • @excelisfun
      @excelisfun  5 лет назад

      Yes, great idea! Thanks, sherry : )

    • @HachiAdachi
      @HachiAdachi 5 лет назад

      ... or simply insert a step:
      Buffer = Table.Buffer(#"Sorted Rows")
      (...which may or may not be easier than prefixing the sort formula with Table.Buffer like sherry did...)

    • @excelisfun
      @excelisfun  5 лет назад +1

      @@HachiAdachi Yes, insert step Table.Buffer is a great way to do it too! Thanks for being part of the Team : )

  • @sadyaz64
    @sadyaz64 5 лет назад +1

    Great vidéo thanks

    • @excelisfun
      @excelisfun  5 лет назад

      You are welcome for the video, sadyaz64!!!!!

  • @bamakaze
    @bamakaze 3 года назад

    Grandmaster Mike! You have helped me become a superhero. Has the Table.Sort been fixed in PQ yet?

    • @excelisfun
      @excelisfun  3 года назад +1

      Not that I know of. But I have not paid too much attention that this specific issue.

  • @rrrprogram8667
    @rrrprogram8667 5 лет назад +3

    I am a huge fannn of DAXxxxxx

    • @excelisfun
      @excelisfun  5 лет назад

      Me too!!!!

    • @rrrprogram8667
      @rrrprogram8667 5 лет назад +1

      @@excelisfun but i guess only 2% of excel users must be using dax... This is bcoz of lot of factors...
      I guess i have to initiate free classes to promote dax in india.. Whats ur advice??

    • @excelisfun
      @excelisfun  5 лет назад

      @@rrrprogram8667 That is a great idea! Post videos at RUclips and tell all your co-workers : )

    • @rrrprogram8667
      @rrrprogram8667 5 лет назад

      @@excelisfun Woww... Your videos make it look so easy to make videos on RUclips... But i understand the hardwork behind it...
      I would rather use ur videos and promote dax.. :D

  • @wayneedmondson1065
    @wayneedmondson1065 5 лет назад

    Hi Mike.. more great stuff. On the formula side, with AGGREGATE, you used 6 (ignore errors) and did a divide on DateTime and the Customer Filter. I experimented and you could also use 4 (ignore nothing) and multiply, as in: =AGGREGATE(14,4,fSales[DateTime]*(fSales[Customer]=F5),3) which in essence is the same as how you constructed the LARGE solution, but without the need for CSE since AGGREGATE can handle it. This is correct.. yes? As a student, I'm trying to understand all the possibilities.. so tinkering for multiple solutions. The PQ and DAX methods are great too. Lots to learn at every turn. Thumbs up!

    • @excelisfun
      @excelisfun  5 лет назад +1

      Yes, large with zeros will work, unless zero in the biggest. It is so great that you are experimenting, learn tons and having fun!!! Me too : )

  • @usedcarsuae.
    @usedcarsuae. 5 лет назад +6

    This I need 😊

    • @excelisfun
      @excelisfun  5 лет назад

      Great, Qasr!!!! Which method do you think you will use?

  • @hi_vishy
    @hi_vishy 2 года назад +1

    Superb video

  • @VITORB82
    @VITORB82 5 лет назад

    My mom says you guys rock!

    • @excelisfun
      @excelisfun  5 лет назад

      I hope the videos do rock for you!!!

  • @johnborg5419
    @johnborg5419 4 года назад +1

    Amazing !!!!

  • @stevennye5075
    @stevennye5075 5 лет назад

    excellent work

    • @excelisfun
      @excelisfun  5 лет назад

      Glad you like it, Steve Nye!!!

  • @mohamedchakroun4973
    @mohamedchakroun4973 5 лет назад

    Veryyy niceeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee tricksssss thanks for all Mikeeee

    • @excelisfun
      @excelisfun  5 лет назад

      You are welcome, Mohamed!!!

  • @wiegunadjatmika6306
    @wiegunadjatmika6306 5 лет назад

    Hi there,
    Could you teach us how to get last value (text) in every single date or others dimension in dax?

  • @armondnazarian4455
    @armondnazarian4455 5 лет назад

    Different solutions are cool

    • @excelisfun
      @excelisfun  5 лет назад

      Yes, different helps to teach : )

  • @hosseinhosseinpoor9561
    @hosseinhosseinpoor9561 3 года назад

    خیلی عالی

  • @williamarthur4801
    @williamarthur4801 2 года назад

    Hi again, I've been trying the p query without grouping, using Table Selectrows, ListMin, ListMaxN,
    your inside, outside trick, no good so far, ho hum.

  • @rrrprogram8667
    @rrrprogram8667 5 лет назад +3

    Bill syzs alwasy has ultra fun with power query

    • @excelisfun
      @excelisfun  5 лет назад +1

      That is soooo true! We love Bill Szysz!!!!

    • @rrrprogram8667
      @rrrprogram8667 5 лет назад

      @@excelisfun wooww... Made ultra sin in spelling his name correctly... Szysz it is.. Haha

  • @rrrprogram8667
    @rrrprogram8667 5 лет назад

    Mike... Have u used one note??.. What is ur take on it... Is it helpful??

  • @Durikru
    @Durikru 5 лет назад

    Благодарю!

  • @666Niusia
    @666Niusia 5 лет назад

    Hello, Formula in Dax works only if there are no repetition of dates (i mean the date for the name is not repetitive /is grouped) what must be done if data are more detailed?

    • @excelisfun
      @excelisfun  5 лет назад

      What is your rule if there are duplicates? If a dup is included, then it is not the last three, right? Also, I do not understand what you mean when you write: "if data are more detailed".

    • @666Niusia
      @666Niusia 5 лет назад

      @@excelisfun i mean if we are using fact table then you have records with the same product duplicated

  • @rrrprogram8667
    @rrrprogram8667 5 лет назад

    Mike.. In a calculated column... Why previousmonth() DOESNOT work.... But month() works... How do we find which function are for calculated coulmsn??

    • @excelisfun
      @excelisfun  5 лет назад

      The only way i can tell which functions work is to: 1) read Microsoft's incomplete and sometimes horrible help, 2) Test and see, 3) think about Filter Context and Row Context and how they interact... As we all know, DAX is not as polite as Worksheet Formulas where we can use the F9 key to see any part of the formula... : (

  • @jamierogers294
    @jamierogers294 5 лет назад

    o_O So does this mean that anything using Table.Sort immediately followed by Table.Group is potentially incorrect after adding new data? :'(

    • @excelisfun
      @excelisfun  5 лет назад

      I do not think so. Only some calculations. I have definitely has sorting, followed by other Power Query Steps where things worked correctly. I have not sure what the trigger is for error, that is what makes it so frustrating... Hopefully Microsoft will fix it.

    • @jamierogers294
      @jamierogers294 5 лет назад +1

      @@excelisfun Thank you! :)

  • @williamarthur4801
    @williamarthur4801 2 года назад

    Before watching DAX, I had a go and used a hurdle so;
    VAR hurdle = MINX( TOPN(3,VALUES(fSales[DateTime]),fSales[DateTime],DESC) ,fSales[DateTime])
    VAR anser = CALCULATE([AvgM], FILTER(ALL(fSales[DateTime]),fSales[DateTime] >= hurdle ) )
    RETURN anser

  • @asanadidy22
    @asanadidy22 5 лет назад

    Can i send you my excel file to help me to summarize

  • @uthamarajk
    @uthamarajk 5 лет назад

    Video games