Forum Discussion
SNRCON81
2 years agoFrequent Visitor
RANKX Over 3 Fields. ID, DATE, TIME & Direction
Hi All, First post, hoping you can help me 🙂 I'm writing some DAX code (Using the DAX query window to test) to create a table that will give me a rank over my ID, Date, Time and direction f...
- 2 years ago
Hi SNRCON81 ,
You can try below dax query.
EVALUATE VAR vTb1 = SELECTCOLUMNS( Sheet1, Sheet1[Id], "Date",FORMAT(Sheet1[Date],"dd/mm/yyyy"), "Time",FORMAT(Sheet1[Time],"hh:mm:ss"), "Index",ROUND(Sheet1[Date]+Sheet1[Time],4)&'Sheet1'[direction], Sheet1[Direction], Sheet1[RANK] ) VAR vTb2 = ADDCOLUMNS( vTb1, "Grp", SUMX( FILTER(vTb1,[Index]<=EARLIER([Index]) && 'Sheet1'[id]=EARLIER('Sheet1'[id])), VAR CurId = 'Sheet1'[id] VAR CurIndex = [Index] VAR CurDirection = 'Sheet1'[direction] VAR PreDirection = MAXX( TOPN(1,FILTER(vTb1,'Sheet1'[id]=CurId && [Index]<CurIndex),[Index]), 'Sheet1'[direction] ) RETURN IF(CurDirection<>PreDirection,1,0) ) ) RETURN ADDCOLUMNS( vTb2, "NEW RANK", RANKX( FILTER(vTb2,'Sheet1'[id]=EARLIER('Sheet1'[id]) && [Grp]=EARLIER([Grp])), [Time],,ASC,Dense ) )Did I answer your question? If yes, pls mark my post as a solution and appreciate your Kudos !
Thank you~
xifeng_L
Super User
2 years agoHi SNRCON81 ,
You can try below dax query.
EVALUATE
VAR vTb1 =
SELECTCOLUMNS(
Sheet1,
Sheet1[Id],
"Date",FORMAT(Sheet1[Date],"dd/mm/yyyy"),
"Time",FORMAT(Sheet1[Time],"hh:mm:ss"),
"Index",ROUND(Sheet1[Date]+Sheet1[Time],4)&'Sheet1'[direction],
Sheet1[Direction],
Sheet1[RANK]
)
VAR vTb2 =
ADDCOLUMNS(
vTb1,
"Grp",
SUMX(
FILTER(vTb1,[Index]<=EARLIER([Index]) && 'Sheet1'[id]=EARLIER('Sheet1'[id])),
VAR CurId = 'Sheet1'[id]
VAR CurIndex = [Index]
VAR CurDirection = 'Sheet1'[direction]
VAR PreDirection =
MAXX(
TOPN(1,FILTER(vTb1,'Sheet1'[id]=CurId && [Index]<CurIndex),[Index]),
'Sheet1'[direction]
)
RETURN
IF(CurDirection<>PreDirection,1,0)
)
)
RETURN
ADDCOLUMNS(
vTb2,
"NEW RANK",
RANKX(
FILTER(vTb2,'Sheet1'[id]=EARLIER('Sheet1'[id]) && [Grp]=EARLIER([Grp])),
[Time],,ASC,Dense
)
)
Did I answer your question? If yes, pls mark my post as a solution and appreciate your Kudos !
Thank you~
- SNRCON812 years agoFrequent Visitor
Excellent!
I've Just tested my proof of concept locally and it worked like a charm
I need to re-produce on the client side and test on a wider dataset, but the solution here works as intended with the above info provided 🙂
Thanks again.