Skip to content

Instantly share code, notes, and snippets.

@tonesandtones
Last active July 18, 2018 09:34
Show Gist options
  • Save tonesandtones/0525b74d8a622c4644c34dbe33274268 to your computer and use it in GitHub Desktop.
Save tonesandtones/0525b74d8a622c4644c34dbe33274268 to your computer and use it in GitHub Desktop.
PowerBI DAX for Index and Value Change fields
--2018-07-18 Updated to use new RANKX function to deal with duplicate timestamp values.
--
--When there are duplicate timestamp values in the dataset, using the previous method (with EARLIER()) would create
--index values that run, eg, 122, 123, 123, 125, 126.
--Supposing therer were 100 new items per row, and ~5000 total (so total would run 4900, 5000, 5100, 5200, etc),
--the ValueChange would run 100, 100, 5000(!!), 100, 100.
--Now, using a RANKX() with Dense enumeration (https://msdn.microsoft.com/en-us/query-bi/dax/rankx-function-dax),
--we can get dense indices that don't skip, so we can lookup Index-1 and know it always exists.
------------------------------------------
--must be DENSE rank so we can lookup value(rank+1) and not get missing values
Index = RANKX(HangfireSuceeded, HangfireSuceeded[timestamp], HangfireSuceeded[timestamp], ASC, Dense)
--Now with dense rank, HangfireSuceeded[Index]-1 is guaranteed to exist
Value Change =
IF (
HangfireSuceeded[Index] = 1,
0,
HangfireSuceeded[value] - LOOKUPVALUE(HangfireSuceeded[value], HangfireSuceeded[Index], HangfireSuceeded[Index]-1)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment