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
depple
Helper III
Helper III

Power Query Editor - Filter n'th high

Hei,

 

In Query Editor, I have a table containing a column like below. As weeks passes, data for the new weeks appears:

 

Week

Wk 2019-01     

Wk 2019-02

Wk 2019-01

Wk 2019-03

Wk 2019-04

Wk 2019-02

Wk 2019-01

Wk 2019-03

 

How do I filter out the n'th highest week. For example, how do I filter, so that only rows with values from the 2nd last week ("Wk 2019-03" in example above) are kept?

 

Thanks in advance for any help.

 

/depple

1 ACCEPTED SOLUTION
v-yuezhe-msft
Employee
Employee

@depple,

Add a blank query in Power BI Desktop, then paste the following code into Advanced Editor of the blank query.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCvdWMDIwtNQ1MFTSUTI0UorVQRIzAooZm6CKGWIRMwaKmaLpNQGZZ4JpnpEhpnkmppjmGRkrxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Week = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Week", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.End([Week],2)),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Value", Int64.Type}, {"Custom", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"Custom", Order.Descending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Custom"}, {{"newcol", each _, type table}}),
    #"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Index", 1, 1),
    #"Expanded newcol" = Table.ExpandTableColumn(#"Added Index", "newcol", {"Week", "Value"}, {"newcol.Week", "newcol.Value"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded newcol", each ([Index] = 2))
in
    #"Filtered Rows"

1.PNG


Regards,
Lydia

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-yuezhe-msft
Employee
Employee

@depple,

Add a blank query in Power BI Desktop, then paste the following code into Advanced Editor of the blank query.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCvdWMDIwtNQ1MFTSUTI0UorVQRIzAooZm6CKGWIRMwaKmaLpNQGZZ4JpnpEhpnkmppjmGRkrxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Week = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Week", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.End([Week],2)),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Value", Int64.Type}, {"Custom", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"Custom", Order.Descending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Custom"}, {{"newcol", each _, type table}}),
    #"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Index", 1, 1),
    #"Expanded newcol" = Table.ExpandTableColumn(#"Added Index", "newcol", {"Week", "Value"}, {"newcol.Week", "newcol.Value"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded newcol", each ([Index] = 2))
in
    #"Filtered Rows"

1.PNG


Regards,
Lydia

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Lydia,

 

Thanks a million!

 

/depple

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.