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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
kai1109
Frequent Visitor

I want to set a flag in the query

Hello. I'm using a translation, so it may be difficult to understand, but thank you for your help.

I would like to set a flag according to the conditions for the following sample data.

 

namevalueMasterCD
【20231218_XXX】sampleA12CD1BCD219CD32CD4-1CD51CD61
【20231218_XXX】sampleA2CD1BCD219CD32CD4-1CD52CD61
【231106_XXX】sampleB12CD1BCD219CD32CD4-1CD51CD61
【231106_XXX】sampleB0CD1BCD219CD32CD4-1CD52CD61

 

Conditions
- It is necessary to judge for each same name.
- If there is a value in MasterCD CD1BCD219CD32CD4-1CD52CD61, set a flag there.
- If CD1BCD219CD32CD4-1CD52CD61 is 0, set a flag in CD1BCD219CD32CD4-1CD51CD61.

In the case of the sample data, I would like to set a flag in the second and third lines.

 

1 ACCEPTED SOLUTION
v-jingzhan-msft
Community Support
Community Support

Hi @kai1109 

 

Here is my solution based on your sample data. Hope it would be helpful!

To see how it works, you can create a blank query, open its Advanced Editor, copy and paste below code to replace any code there. 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WetwwwcjAyNjQyNAiPiIi4nHDxOLE3IKcVEclHSVDIyDh7GLo5OxiZGjp7GJs5Oxiomvo7GIKxGaGSrE6+PXj1G6Eot3Y0NDADEWzEwmWY9dtQMDqWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [name = _t, value = _t, MasterCD = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"name", type text}, {"value", Int64.Type}, {"MasterCD", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"name"}, {{"All", each _, type table [name=nullable text, value=nullable number, MasterCD=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Condition3", each Table.SelectRows([All], each [MasterCD]="CD1BCD219CD32CD4-1CD52CD61" and [value]=0)),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom", each let vCondition3=Table.IsEmpty([Condition3]) in Table.AddColumn([All], "Flag", each if [MasterCD]="CD1BCD219CD32CD4-1CD52CD61" then (if [value]=0 then null else if [value] <> null then 1 else null) else if [MasterCD] = "CD1BCD219CD32CD4-1CD51CD61" then (if vCondition3=true then null else 1) else null)),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"name", "value", "MasterCD", "Flag"}, {"name", "value", "MasterCD", "Flag"})
in
    #"Expanded Custom"

Result 

vjingzhanmsft_0-1717033846349.png

 

Best Regards,
Jing
If this post helps, please Accept it as Solution to help other members find it. Appreciate your Kudos!

View solution in original post

2 REPLIES 2
dufoq3
Super User
Super User

Hi @kai1109, different approach here:

 

Result

dufoq3_0-1717049174401.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WetwwwcjAyNjQyNAiPiIi4nHDxOLE3IKcVEclHSVDIyDh7GLo5OxiZGjp7GJs5Oxiomvo7GIKxGaGSrE6+PXj1G6Eot3Y0NDADEWzEwmWY9dtQMDqWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [name = _t, value = _t, MasterCD = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"name", type text}, {"value", Int64.Type}, {"MasterCD", type text}}),
    GroupedRows = Table.Group(ChangedType, {"name"}, {{"All", each
        Table.AddColumn(_, "Flag", (x)=>
            if x[MasterCD] = "CD1BCD219CD32CD4-1CD52CD61" and x[value] <> 0 then 1 else
            if x[MasterCD] = "CD1BCD219CD32CD4-1CD51CD61" and Table.SelectRows(_, (y)=> y[MasterCD] = "CD1BCD219CD32CD4-1CD52CD61")[value]{0}? = 0 then 1
            else 0, Int64.Type), type table}}),
    CombinedAll = Table.Combine(GroupedRows[All])
in
    CombinedAll

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

v-jingzhan-msft
Community Support
Community Support

Hi @kai1109 

 

Here is my solution based on your sample data. Hope it would be helpful!

To see how it works, you can create a blank query, open its Advanced Editor, copy and paste below code to replace any code there. 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WetwwwcjAyNjQyNAiPiIi4nHDxOLE3IKcVEclHSVDIyDh7GLo5OxiZGjp7GJs5Oxiomvo7GIKxGaGSrE6+PXj1G6Eot3Y0NDADEWzEwmWY9dtQMDqWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [name = _t, value = _t, MasterCD = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"name", type text}, {"value", Int64.Type}, {"MasterCD", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"name"}, {{"All", each _, type table [name=nullable text, value=nullable number, MasterCD=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Condition3", each Table.SelectRows([All], each [MasterCD]="CD1BCD219CD32CD4-1CD52CD61" and [value]=0)),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom", each let vCondition3=Table.IsEmpty([Condition3]) in Table.AddColumn([All], "Flag", each if [MasterCD]="CD1BCD219CD32CD4-1CD52CD61" then (if [value]=0 then null else if [value] <> null then 1 else null) else if [MasterCD] = "CD1BCD219CD32CD4-1CD51CD61" then (if vCondition3=true then null else 1) else null)),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"name", "value", "MasterCD", "Flag"}, {"name", "value", "MasterCD", "Flag"})
in
    #"Expanded Custom"

Result 

vjingzhanmsft_0-1717033846349.png

 

Best Regards,
Jing
If this post helps, please Accept it as Solution to help other members find it. Appreciate your Kudos!

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! Prices go up Feb. 11th.

Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.