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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
erikmathijsen
New Member

Selective row based on many scenarios

erikmathijsen_0-1713945420252.png

 

See my table above, which is just a subset of many rows.

What I'm trying to do is only listing the correct row based on SKU & Type for the related Privilege.

Privilege is important and determine which SKU is triggered. In case that we see a duplicate Privilege then we only want to return the SKU where Type = "Contracted"

If there are no duplicate privileges then we can return the single row

 

Expected outcome:

 

erikmathijsen_1-1713945575096.png

 

2 ACCEPTED SOLUTIONS
_AAndrade
Super User
Super User

Hi @erikmathijsen,

In your example what was the criteria that you used to choose the first row of SKU B67293 instead of second row? 
Both rows have type = Unexpected?

 





Did I answer your question? Mark my post as a solution! Kudos are welcome.

Proud to be a Super User!




View solution in original post

Hi @erikmathijsen ,

Here is my solution:

1. I'm using this data set:

_AAndrade_1-1713956980561.png


2. My M script code is this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCvYOVdJRCsrPSXXOT0kFMgOKMssycxLTQeyQyoJUpVidaCUnM3MjS2OgiGNAfLh/kLdrULyXvxMKPyDIMwwoEOrnGhHg6hzi6oKh0dfRz9EdWSdMwDHI1RGXfgtDU0MDEvQ7+/uFBDli1Q9yqI+/owuq08EiEUA73XEZYWloYGpOqt9jAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    PromotedHeaders = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    ChangedType = Table.TransformColumnTypes(PromotedHeaders,{{"SKU", type text}, {"RoleCode", type text}, {"Privilage", type text}, {"Type", type text}}),
    GroupTable = Table.Group(ChangedType, {"Privilage"}, {
        {"GroupTable", each if List.ContainsAny([Type], {"CONTRACTED"}) then Table.SelectRows(_, each [Type] = "CONTRACTED") else _}
    }),
    ExpandTable = Table.ExpandTableColumn(GroupTable, "GroupTable", {"SKU", "RoleCode", "Privilage", "Type"}, {"SKU", "RoleCode", "Privilage.1", "Type"}),
    RemoveColumns = Table.RemoveColumns(ExpandTable,{"Privilage"}),
    RenameColumn = Table.RenameColumns(RemoveColumns,{{"Privilage.1", "Privilage"}}),
    Result = Table.TransformColumnTypes(RenameColumn,{{"Privilage", type text}, {"SKU", type text}, {"RoleCode", type text}, {"Type", type text}})
in
    Result


3. Final Output:

_AAndrade_2-1713957044502.png

 

 





Did I answer your question? Mark my post as a solution! Kudos are welcome.

Proud to be a Super User!




View solution in original post

7 REPLIES 7
dufoq3
Super User
Super User

Hi, different approach:

You can delete AddedIndex and SortedRows steps if sort order doesn't matter.

 

Result

dufoq3_0-1713972627217.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjIzN7I0VtJRcgyID/cP8nYNig8I8gwDCoT6uUYEuDqHuLooxeqgKvR19HN0B6p0DHJ1xKXcwtDU0AC3cmd/v5AgR6zKQc7w8Xd0iY8AGuiOS4OloYGpOSF3xwIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [SKU = _t, Privilege = _t, Type = _t]),
    AddedIndex = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    GroupedRows = Table.Group(AddedIndex, {"Privilege"}, {{"All", each 
        [ a = Table.SelectRows(_, (x)=> x[Type] = "CONTRACTED"),
          b = if Table.RowCount(a) > 0 then a else _
        ][b], type table}}),
    CombinedAll = Table.Combine(GroupedRows[All]),
    SortedRows = Table.Sort(CombinedAll,{{"Index", Order.Ascending}})
in
    SortedRows

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

erikmathijsen
New Member

Thank you very much !!

erikmathijsen
New Member

The criteria is that there is no "Contracted" SKU available, therefore the "Unexpected" SKU can be listed.

The privileges can be found against many SKUs, but I only have contracted only a few SKUs, which I've set to "Contracted"

In case a privilege is used, it should first try to find the Contracted SKU, if that is not available then it should list all Unexpected SKUs.

The combination SKU & TYpe = "Contracted" is unique.

Sorry but I'm not getting your ideia.

According of your rules your output must show the two rows for SKU B67293 and not only one row.

The only thing I'm thinking is you are using role column to distinct instead of sku





Did I answer your question? Mark my post as a solution! Kudos are welcome.

Proud to be a Super User!




Let me try to explain, therefore adding one more row..

 

erikmathijsen_0-1713951958084.png

 

Type = contracted means that we have purchased this SKU and each SKU has Privileges (1 or more).

And end user can be assigned to Privileges, which could trigger multiple SKUs.

If the Privilege is identified against a Contracted SKU, then all Unexpected SKUs can be ignored.

If the Privilege is identified against Unexpected SKU(s) then all Unexpected SKU(s) need to be listed.

 

Additional Explanation

AP_WORKER_PRIV is listed twice (B67293 & B91057), both of Type = Unexpected.  Because we don't have a SKU, where type is "Contracted" for this Privilege, both rows need to be displayed.

 

AP_MANAGER_AREA_PRIV is listed twice (B67293 <Type ="Unexpected"> & B81510 <Type = "Contracted".   In this case we are only interested in the "Contracted SKU.

 

Hope this explains the logic/rational behind my question.

 

 

Hi @erikmathijsen ,

Here is my solution:

1. I'm using this data set:

_AAndrade_1-1713956980561.png


2. My M script code is this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCvYOVdJRCsrPSXXOT0kFMgOKMssycxLTQeyQyoJUpVidaCUnM3MjS2OgiGNAfLh/kLdrULyXvxMKPyDIMwwoEOrnGhHg6hzi6oKh0dfRz9EdWSdMwDHI1RGXfgtDU0MDEvQ7+/uFBDli1Q9yqI+/owuq08EiEUA73XEZYWloYGpOqt9jAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    PromotedHeaders = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    ChangedType = Table.TransformColumnTypes(PromotedHeaders,{{"SKU", type text}, {"RoleCode", type text}, {"Privilage", type text}, {"Type", type text}}),
    GroupTable = Table.Group(ChangedType, {"Privilage"}, {
        {"GroupTable", each if List.ContainsAny([Type], {"CONTRACTED"}) then Table.SelectRows(_, each [Type] = "CONTRACTED") else _}
    }),
    ExpandTable = Table.ExpandTableColumn(GroupTable, "GroupTable", {"SKU", "RoleCode", "Privilage", "Type"}, {"SKU", "RoleCode", "Privilage.1", "Type"}),
    RemoveColumns = Table.RemoveColumns(ExpandTable,{"Privilage"}),
    RenameColumn = Table.RenameColumns(RemoveColumns,{{"Privilage.1", "Privilage"}}),
    Result = Table.TransformColumnTypes(RenameColumn,{{"Privilage", type text}, {"SKU", type text}, {"RoleCode", type text}, {"Type", type text}})
in
    Result


3. Final Output:

_AAndrade_2-1713957044502.png

 

 





Did I answer your question? Mark my post as a solution! Kudos are welcome.

Proud to be a Super User!




_AAndrade
Super User
Super User

Hi @erikmathijsen,

In your example what was the criteria that you used to choose the first row of SKU B67293 instead of second row? 
Both rows have type = Unexpected?

 





Did I answer your question? Mark my post as a solution! Kudos are welcome.

Proud to be a Super User!




Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors