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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
sureshg2498
Advocate II
Advocate II

Adding Flag (Y) to first row of each Region

Hi All,

 

We have a requirement to add a flag=Y to only first row of each region as shown below.

 

RegionProductSalesFlag
SouthiPhone200Y
SouthSamsung300N
SouthMoto600N
SouthSony700N
EastSamsung800Y
EastMoto200N
WestMoto1000Y
WestSony2000N

 

Could anyone please help me on it?

 

 

Thanks,

Suresh.

@Bibiano_Geraldo @rajendraongole1 @Ritaf1983 @danextian  @lbendlin @Ritaf1983 @bhanu_gautam

1 ACCEPTED SOLUTION

This should be straight forward
A Calculated column for ranking the Products:

ProductRank = 
SWITCH(
    'Flag_Table'[Product],
    "iPhone", 1,
    "Samsung", 2,
    "Moto", 3,
    "Sony", 4,
    99  
)


If you need to do this using a measure:

Flag Product Measure = 
VAR CurrentRegion = SELECTEDVALUE('Flag_Table'[Region])
VAR CurrentRank = SELECTEDVALUE('Flag_Table'[ProductRank])
VAR MinRankInRegion =
    CALCULATE(
        MIN('Flag_Table'[ProductRank]),
        FILTER(
            ALL('Flag_Table'),
            'Flag_Table'[Region] = CurrentRegion
        )
    )
RETURN
    IF(CurrentRank = MinRankInRegion, "Y", "N")


If you need a calculated column:

Flag Product = 
VAR CurrentRegion = 'Flag_Table'[Region]
VAR CurrentRank = 'Flag_Table'[ProductRank]
VAR MinRankInRegion =
    CALCULATE(
        MIN('Flag_Table'[ProductRank]),
        FILTER(
            ALL('Flag_Table'),
            'Flag_Table'[Region] = CurrentRegion
        )
    )
RETURN
    IF(CurrentRank = MinRankInRegion, "Y", "N")


The Product Ranking Column need not be added to the visual, either the measure or the Calculated column would help you in flagging as required

View solution in original post

4 REPLIES 4
MohamedFowzan1
Solution Specialist
Solution Specialist

Hi @sureshg2498 

Whats the exact logic is it by the sort order, highest sales, lowest sales?

Assuming you create a Sort Column/Index:
Calculated Column:

Flag = 
VAR CurrentRegion = Flag_Table[Region]
VAR CurrentSort = Flag_Table[Sort]
VAR MinSort =
    CALCULATE(
        MIN(Flag_Table[Sort]),
        FILTER(
            ALL(Flag_Table),
            Flag_Table[Region] = CurrentRegion
        )
    )
RETURN
    IF(CurrentSort = MinSort, "Y", "N")


Measure: 

Flag Measure = 
VAR CurrentRegion = SELECTEDVALUE(Flag_Table[Region])
VAR CurrentSort = SELECTEDVALUE(Flag_Table[Sort])
VAR MinSort =
    CALCULATE(
        MIN(Flag_Table[Sort]),
        FILTER(
            ALL(Flag_Table),
            Flag_Table[Region] = CurrentRegion
        )
    )
RETURN
    IF(CurrentSort = MinSort, "Y", "N")

MohamedFowzan1_0-1754395101872.png

If you want to achieve in Powerquery instead here is the MCode:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCs4vLclQ0lHKDMjIz0sFMowMDJRidRASwYm5xaV56UCWMZqMb35JPpAyQ9eQn1cJpMyhwq6JxSUoxligSkBNgVkbnoosamiAKgw12wgsHAsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Region = _t, Product = _t, Sales = _t]),

    #"Grouped Rows" = Table.Group(Source, {"Region"}, {{"Grouped", each _, type table [Region=nullable text, Product=nullable text, Sales=nullable text]}}),

    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "FlagIndex", each Table.AddIndexColumn([Grouped], "FlagIndex", 1, 1)),

    #"Added Flag" = Table.AddColumn(#"Added Custom", "Flagged", each Table.AddColumn([FlagIndex], "Flag", each if [FlagIndex] = 1 then "Y" else "N")),

    #"Expanded Flagged" = Table.ExpandTableColumn(#"Added Flag", "Flagged", {"Product", "Sales", "Flag"}),

    Result = Table.RemoveColumns(#"Expanded Flagged", {"Grouped", "FlagIndex"})
in
    Result

MohamedFowzan1_1-1754395534386.png

Basically first Group by Region:

MohamedFowzan1_2-1754395595917.png

Add a custom column:

MohamedFowzan1_3-1754395615181.png

Add the flag:

MohamedFowzan1_4-1754395638617.png


Then expand the flag and remove unnecessary columns.

If Resolved Mark as Solution to guide Others!

 

 

Thank you for the reply Mohamed. We just need to add the Flag to the first row of each region. There is a custom sort on Product Name. Below is the Products order for each region.Some regions may not have all products.

 

 

iPhone

Samsung

Moto

Sony

Whatever the Product is present in the first row just need to add the flag as Y.

This should be straight forward
A Calculated column for ranking the Products:

ProductRank = 
SWITCH(
    'Flag_Table'[Product],
    "iPhone", 1,
    "Samsung", 2,
    "Moto", 3,
    "Sony", 4,
    99  
)


If you need to do this using a measure:

Flag Product Measure = 
VAR CurrentRegion = SELECTEDVALUE('Flag_Table'[Region])
VAR CurrentRank = SELECTEDVALUE('Flag_Table'[ProductRank])
VAR MinRankInRegion =
    CALCULATE(
        MIN('Flag_Table'[ProductRank]),
        FILTER(
            ALL('Flag_Table'),
            'Flag_Table'[Region] = CurrentRegion
        )
    )
RETURN
    IF(CurrentRank = MinRankInRegion, "Y", "N")


If you need a calculated column:

Flag Product = 
VAR CurrentRegion = 'Flag_Table'[Region]
VAR CurrentRank = 'Flag_Table'[ProductRank]
VAR MinRankInRegion =
    CALCULATE(
        MIN('Flag_Table'[ProductRank]),
        FILTER(
            ALL('Flag_Table'),
            'Flag_Table'[Region] = CurrentRegion
        )
    )
RETURN
    IF(CurrentRank = MinRankInRegion, "Y", "N")


The Product Ranking Column need not be added to the visual, either the measure or the Calculated column would help you in flagging as required

Thank you so much, Mohamed! Your explanation is incredibly clear and helped me solve the issue right away. I really appreciate the time you took to break it down.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors