Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Hello, i'm trying to find a way to flag first apprence of a duplicates across muliple columns using DAX, my table looks like this:
ID DATE FLAG
1 01/01/2011 1
1 01/01/2011 0
2 02/02/2011 1
2 02/02/2011 0
2 02/02/2011 0
I tried with this calculated column but didn't work:
Solved! Go to Solution.
@uif19085 can you please test out the following meaure , you probably need a rowNum/Index Column
dup =
VAR __val =
CALCULATE (
[current],
OFFSET (
-1,
DISTINCT ( ALL ( 'Table' ) ),
ORDERBY ( 'Table'[Index] ),
KEEP,
PARTITIONBY ( 'Table'[id] )
)
)
VAR currentId =
MAX ( 'Table'[id] )
VAR currentIndex =
MAX ( 'Table'[Index] )
VAR dateCount =
CALCULATE (
DISTINCTCOUNT ( 'Table'[date] ),
FILTER (
ALL ( 'Table' ),
'Table'[id] = currentId
&& 'Table'[Index] <= currentIndex
)
)
VAR final =
SWITCH (
TRUE (),
__val == BLANK (), 1,
[current] - 1 = __val
&& dateCount = 1, 0,
1
)
RETURN
final
or like this
Measure =
VAR currDate =
MAX ( 'Table'[date] )
VAR currDate2 = currDate + 0
VAR currentId =
MAX ( 'Table'[id] )
VAR currentIndex =
MAX ( 'Table'[Index] )
VAR minIndexbyID =
CALCULATE (
[currentIndex],
INDEX (
1,
DISTINCT ( ALL ( 'Table' ) ),
ORDERBY ( 'Table'[Index] ),
KEEP,
PARTITIONBY ( 'Table'[id] )
)
)
VAR filterTbl1 =
FILTER (
ALL ( 'Table' ),
'Table'[id] = currentId
&& 'Table'[Index] >= minIndexbyID
&& currentIndex >= 'Table'[Index]
)
VAR filterTbl2 =
FILTER (
ALL ( 'Table' ),
'Table'[id] = currentId
&& 'Table'[Index] >= minIndexbyID
)
VAR leftTbl1 =
SELECTCOLUMNS ( filterTbl1, "dt", 'Table'[date] + 0 )
VAR leftTbl2 =
SELECTCOLUMNS ( filterTbl2, "dt", 'Table'[date] + 0 )
VAR rightTbl =
SELECTCOLUMNS ( { currDate }, "dt", [Value] + 0 )
VAR dates1 =
NATURALINNERJOIN ( leftTbl1, rightTbl )
VAR dates2 =
NATURALINNERJOIN ( leftTbl2, rightTbl )
VAR val1 =
COUNTROWS ( dates1 )
VAR val2 =
COUNTROWS ( dates2 )
VAR ternary =
SWITCH (
TRUE (),
val1 = 1
&& val2 = 1, val1 & "-unique entry",
val1 = 1
&& val2 <> 1, val1 & "-has duplicate entry",
"0" & "-has duplicate entry"
)
RETURN
ternary
Hi,
It may be easier to do this in the Query Editor. Would you be interested in that solution?
Hi , @uif19085
According to your description, you want to "Flag duplicates with DAX".
First, to distinguish this difference, we need a column to distinguish the order of the data, and we need to add an index column to the Power Query.
Then we can add a calculated column use this dax code:
Column = var _t =FILTER('Table','Table'[ID]=EARLIER('Table'[ID]) && 'Table'[DATE]=EARLIER('Table'[DATE]) && 'Table'[Index]<= EARLIER('Table'[Index]))
return
IF(COUNTROWS(_t)=1,1,0)
Then we can meet your need:
Thank you for your time and sharing, and thank you for your support and understanding of PowerBI!
Best Regards,
Aniya Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly
@uif19085 can you please test out the following meaure , you probably need a rowNum/Index Column
dup =
VAR __val =
CALCULATE (
[current],
OFFSET (
-1,
DISTINCT ( ALL ( 'Table' ) ),
ORDERBY ( 'Table'[Index] ),
KEEP,
PARTITIONBY ( 'Table'[id] )
)
)
VAR currentId =
MAX ( 'Table'[id] )
VAR currentIndex =
MAX ( 'Table'[Index] )
VAR dateCount =
CALCULATE (
DISTINCTCOUNT ( 'Table'[date] ),
FILTER (
ALL ( 'Table' ),
'Table'[id] = currentId
&& 'Table'[Index] <= currentIndex
)
)
VAR final =
SWITCH (
TRUE (),
__val == BLANK (), 1,
[current] - 1 = __val
&& dateCount = 1, 0,
1
)
RETURN
final
or like this
Measure =
VAR currDate =
MAX ( 'Table'[date] )
VAR currDate2 = currDate + 0
VAR currentId =
MAX ( 'Table'[id] )
VAR currentIndex =
MAX ( 'Table'[Index] )
VAR minIndexbyID =
CALCULATE (
[currentIndex],
INDEX (
1,
DISTINCT ( ALL ( 'Table' ) ),
ORDERBY ( 'Table'[Index] ),
KEEP,
PARTITIONBY ( 'Table'[id] )
)
)
VAR filterTbl1 =
FILTER (
ALL ( 'Table' ),
'Table'[id] = currentId
&& 'Table'[Index] >= minIndexbyID
&& currentIndex >= 'Table'[Index]
)
VAR filterTbl2 =
FILTER (
ALL ( 'Table' ),
'Table'[id] = currentId
&& 'Table'[Index] >= minIndexbyID
)
VAR leftTbl1 =
SELECTCOLUMNS ( filterTbl1, "dt", 'Table'[date] + 0 )
VAR leftTbl2 =
SELECTCOLUMNS ( filterTbl2, "dt", 'Table'[date] + 0 )
VAR rightTbl =
SELECTCOLUMNS ( { currDate }, "dt", [Value] + 0 )
VAR dates1 =
NATURALINNERJOIN ( leftTbl1, rightTbl )
VAR dates2 =
NATURALINNERJOIN ( leftTbl2, rightTbl )
VAR val1 =
COUNTROWS ( dates1 )
VAR val2 =
COUNTROWS ( dates2 )
VAR ternary =
SWITCH (
TRUE (),
val1 = 1
&& val2 = 1, val1 & "-unique entry",
val1 = 1
&& val2 <> 1, val1 & "-has duplicate entry",
"0" & "-has duplicate entry"
)
RETURN
ternary
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
109 | |
98 | |
80 | |
64 | |
57 |
User | Count |
---|---|
145 | |
111 | |
92 | |
84 | |
66 |