Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
deannapi
Frequent Visitor

Duplicate IDs with criteria

I have a table with an ID column, a column "Origin" and a date column.

IDOriginDate

12

Web1/1/2024

12

Other1/2/2024
13Other1/2/2024
14Web1/3/2024
14Other1/3/2024
15Other1/3/2024
15Web1/4/2024
15Other1/4/2024
16Other1/4/2024
17Web1/4/2024
17Web1/5/2024
18Web1/5/2024
18Other1/5/2024

If an ID is repeated in one day and one of the origins = "Other", then I want that ID to be "Other", not "web".  Basically, "Other" trumps "Web" if an ID belongs to both in the same day.

 

How can get a table to reflect this via DAX, not power query? I have no code, I don't know what to even try. 

1 ACCEPTED SOLUTION
adudani
Super User
Super User

hi @deannapi ,

 

create a blank query and copy paste the below code into the advanced editor

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjRS0lEKT00Ckob6hvpGBkYmSrE6UHH/kozUIrCMEZKMMU4ZEySzjNHEETqQZUzxysDMMsGpA1nGDKeMOQ6zkMVNkcQt8IgjbIDJxAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Origin = _t, Date = _t]),
    //merge date and id columns
    #"Date-ID concatenation" = Table.AddColumn(Source, "Date -ID", each Text.Combine({[Date], [ID]}, " - "), type text),
    #"Changed Type" = Table.TransformColumnTypes(#"Date-ID concatenation",{{"ID", Int64.Type}, {"Origin", type text}, {"Date", type date}}),
    // group all data by date and id column
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Date -ID"}, {{"Data", each _, type table [ID=nullable number, Origin=nullable text, Date=nullable date, #"Date -ID"=text]}}),
    // criteria to check if multiple ids in the same day
    ReplaceID = Table.AddColumn(#"Grouped Rows", "ReplaceID", each not( List.Count([Data][ID]) = List.Count(List.Distinct([Data][ID])))),
    // Get data which doesn't need to be replaced. Also, replace Origin that has multiple ids in same day
    Data_New = Table.AddColumn(ReplaceID, "Data_New", each if [ReplaceID]= true then Table.AddColumn(Table.RemoveColumns([Data],"Origin"),"Origin", each "Other") else [Data]),
    #"Removed Other Columns" = Table.SelectColumns(Data_New,{"Data_New"}),
    #"Expanded Data_New" = Table.ExpandTableColumn(#"Removed Other Columns", "Data_New", {"ID", "Origin", "Date"}, {"ID", "Origin", "Date"})
in
    #"Expanded Data_New"

tried to solve this via Power Query. let me know if this is the expected output below:

 

adudani_0-1711572806540.png

steps:

1. created a merged date-id column

2. group all data by this column

3. applied a check to see if there are multiple ids on that date-id combination

4. if yes, replace "Web" with "Other", else give the Data without replacement.

5. expanded the "New_Data" Table with the changes.

 

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a Kudos 🙂
Kind Regards,
Avinash

View solution in original post

2 REPLIES 2
tamerj1
Super User
Super User

Hi @deannapi 

for a DAX solution you my try the following calculated table

NewTable =
ADDCOLUMNS (
SUMMARIZE ( 'Table', 'Table'[ID], 'Table'[Date] ),
"Origin", CALCULATE ( MIN ( 'Table'[Origin] ) )
)

adudani
Super User
Super User

hi @deannapi ,

 

create a blank query and copy paste the below code into the advanced editor

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjRS0lEKT00Ckob6hvpGBkYmSrE6UHH/kozUIrCMEZKMMU4ZEySzjNHEETqQZUzxysDMMsGpA1nGDKeMOQ6zkMVNkcQt8IgjbIDJxAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Origin = _t, Date = _t]),
    //merge date and id columns
    #"Date-ID concatenation" = Table.AddColumn(Source, "Date -ID", each Text.Combine({[Date], [ID]}, " - "), type text),
    #"Changed Type" = Table.TransformColumnTypes(#"Date-ID concatenation",{{"ID", Int64.Type}, {"Origin", type text}, {"Date", type date}}),
    // group all data by date and id column
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Date -ID"}, {{"Data", each _, type table [ID=nullable number, Origin=nullable text, Date=nullable date, #"Date -ID"=text]}}),
    // criteria to check if multiple ids in the same day
    ReplaceID = Table.AddColumn(#"Grouped Rows", "ReplaceID", each not( List.Count([Data][ID]) = List.Count(List.Distinct([Data][ID])))),
    // Get data which doesn't need to be replaced. Also, replace Origin that has multiple ids in same day
    Data_New = Table.AddColumn(ReplaceID, "Data_New", each if [ReplaceID]= true then Table.AddColumn(Table.RemoveColumns([Data],"Origin"),"Origin", each "Other") else [Data]),
    #"Removed Other Columns" = Table.SelectColumns(Data_New,{"Data_New"}),
    #"Expanded Data_New" = Table.ExpandTableColumn(#"Removed Other Columns", "Data_New", {"ID", "Origin", "Date"}, {"ID", "Origin", "Date"})
in
    #"Expanded Data_New"

tried to solve this via Power Query. let me know if this is the expected output below:

 

adudani_0-1711572806540.png

steps:

1. created a merged date-id column

2. group all data by this column

3. applied a check to see if there are multiple ids on that date-id combination

4. if yes, replace "Web" with "Other", else give the Data without replacement.

5. expanded the "New_Data" Table with the changes.

 

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a Kudos 🙂
Kind Regards,
Avinash

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.