An Attempt to Use New OFFSET Function in DAX to Find Previous/Next Values in a Visual in Power BI

An Attempt to Use New OFFSET Function in DAX to Find Previous/Next Values in a Visual in Power BI

It's a bit strange to write about a new DAX function, that doesn't yet appear even in the intellisense in Power BI Desktop! And also there is no documentation about it yet! What tempted me is what I saw on Twitter last week, here and here!

No alt text provided for this image

In a retweet from Chris Webb , a bunch of DAX code showing values for previous row in a matrix without relying on index-like solutions! (Original tweet by Takeshi Kagata )

Since DAX does not have a cell-based reference like Excel, it's not always easy to capture values from previous row in a visual. Most common methods include using index-columns or using functions such as EARLIER.

I wrote a blog post almost 3 years ago, on my blog PowerBI.Istanbul, showing how to find previous week sales value in a matrix, where I ended up with 8 lines of DAX code, like this one:

Previous Week Sales :=
IF( HASONEVALUE( 'Calendar'[Week No] ) ,
? ? CALCULATE( [Sales] ,
? ? ? ? FILTER( ALL( 'Calendar' ) ,
? ? ? ? 'Calendar'[Week Index] = MAX( 'Calendar'[Week Index] ) - 1 
? ? ? ? )
? ? )
)         

HASONEVALUE part of the code is just to show measure only at week no level. The rest of the code relies on indexing logic set on additional Week Index column, where I assign unique values for each and every week in Calendar table. Not a rocket science if you are familiar with DAX and you know how iterators work!

Similar previous week calculation is now also achievable using OFFSET function. The code below will calculate the same values, just in a different way. (Red lines just indicate that intellisense doesnt recognize it yet!)

No alt text provided for this image
No alt text provided for this image

I didn't make comprehensive tests, but the performance of these 2 different measures are quite similar, at least for my dataset. (I use Contoso database from MS)

So the question is, if it's already achievable with the current DAX abilities, what does that brand new DAX function bring us new! Well, if you are able to setup indexing logic like the one above, then fine, you may continue using it! But how about the matrix below?

No alt text provided for this image

It's a piece of cake if we are using Excel, but not in DAX!

You have to setup some sort of logic to achieve this and it's quite likely that your DAX code won't be a short one!

The code below, with the help of new OFFSET function will do this for us!

No alt text provided for this image

I noticed that removing the ORDERBY line from the code doesn't make any difference, so it should be an optional paramater. However, that ORDERBY part has also an option to choose the direction of calculation!

No alt text provided for this image

I'm a little bit confused at this point, because I'm not able to figure out what to do if user changes the sort order from the visual! For example, if user sorts the matrix according to Sales measure in increasing order, then the matrix looks like this!

No alt text provided for this image

To my knowledge, there is no way to capture how the visual is sorted as of today!

...

I read the following in one of the responses to the tweet, from Jeroen (Jay) ter Heerdt , member of Power BI CAT team!

No alt text provided for this image

"Relative movement in the result set" ! That's a bold sentence!

I'm keen to find out more about this new DAX function! Eagerly waiting for more information on this.

Cheers.

Benoit Fedit

BI Tech Lead at MSC | Blog Datakuity.com

2 年

great post Halil Gungormus! For the visual sorting, I first thought that it would recompute the measure and change the result according to the visual order, but it makes more sense that the result of the measure is still based on the reusltset order and not impacted by the visual order. Actually, it would've been awkward to try sorting the measure "previous category sales" by itself :)

Jeroen (Jay) ter Heerdt

Product Manager for Power BI. Speaker. Dutch Data Dude.

2 年

Thanks for sharing! Couple of things : partitionBy is another optional parameter that allows you to specify the sort order. Once intellisense is there you will be able to use this successfully, no doubt. Also, I am not on the CAT team, but actually on the product team. I am the Product Manager for DAX and Modeling, which includes the Offset function so any feedback you have is very welcome!

要查看或添加评论,请登录

Halil Gungormus的更多文章

  • How to Model Dynamics 365 F&O Product Category Tables in Power BI

    How to Model Dynamics 365 F&O Product Category Tables in Power BI

    If you are developing Power BI models on Dynamics F&O, then you might be familiar with EcoRes tables. These are the…

    12 条评论
  • How to do Price Volume Mix Analysis in Power BI

    How to do Price Volume Mix Analysis in Power BI

    If you are working for a retail or FMCG company, then one of the techniques that you should definitely employ in your…

    13 条评论
  • Power BI + AI

    Power BI + AI

    Power BI her ay güncelleniyor, dura?an bir uygulama kesinlikle de?il. Son d?nemde duyurulan yeniliklerden biri yak?n…

    3 条评论
  • Hangi Metrikler? Holistik bir bak??!

    Hangi Metrikler? Holistik bir bak??!

    Yakla??k iki y?ld?r, i? analiti?i ile ilgili ?al???yorum. Microsoft Power BI ?zelinde teknik yaz?lar yay?mlad???m bir…

  • Veri G?rselle?tirme

    Veri G?rselle?tirme

    PowerBI.?stanbul - Blog Excel ?ok yayg?n kullan?lan bir uygulama.

    1 条评论
  • Power BI, QlikView, Tableau

    Power BI, QlikView, Tableau

    Bu bir “di?er uygulamalar? ve pratikleri k?tüleme” yaz?s? de?ildir, ba?tan s?ylemi? olay?m. Power BI projelerinde…

    4 条评论
  • Zey.Ya?.Taz.Fas

    Zey.Ya?.Taz.Fas

    Internet ?a??nday?z, hepimizin cebinde üstelik, bilgiye ula?mak art?k ?ok kolay ve h?zl?. De?il mi? De?il.

    1 条评论
  • To Viz or Not to Viz, ya da Godzilla

    To Viz or Not to Viz, ya da Godzilla

    # Veri, anlaml? bilgiye d?nü?türülebildi?i zaman k?ymetli. Peki anlaml? bilgi ne demek? Herkesin buna kendine g?re bir…

  • Zarf ile mazruf, sat??lar? il il haritada g?steren Excel dosyas?, ve dahi Endüstri 4.0

    Zarf ile mazruf, sat??lar? il il haritada g?steren Excel dosyas?, ve dahi Endüstri 4.0

    # K?sa notlar halinde yazaca??m, zira konu biraz uzun, hem karma??k -gibi- , hem de basit. ?laveten, bo? bulunup okumak…

  • Olymp ve Faniler

    Olymp ve Faniler

    Hemen her konunun bir "gurusunun" oldu?u y?llarday?z uzun süredir. En bi' ?ahane lider olmak i?in yapman?z gereken top…

社区洞察

其他会员也浏览了