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
transform99
Resolver I
Resolver I

Getting most recent file based on reporting Month with different file names

I have 3 columns in Power BI with similar naming conventions to below. There are 2 distinct naming conventions:

- Weekly File MmmYY

- Washed File MmmYY

 

If a washed file exists for the month, I only want to return that file. If a washed file does not exist for that month, I want to return the most recent Weekly file available. How can this be done?

 

NameDate CreatedReport Month
Weekly File Nov231/11/20231/11/2023
Weekly File Nov238/11/20231/11/2023
Weekly File Nov2315/11/20231/11/2023
Weekly File Nov2322/11/20231/11/2023
Weekly File Nov2329/11/20231/11/2023
Washed File Nov236/12/20231/11/2023
Weekly File Dec236/12/20231/12/2023
Weekly File Dec2313/12/20231/12/2023
Weekly File Dec2320/12/20231/12/2023
Weekly File Dec2327/12/20231/12/2023
Weekly File Jan243/01/20241/01/2024
Washed File Dec233/01/20241/12/2023
Weekly File Jan2410/01/20241/01/2024

 

In the above table the following would be correct files chosen:

november = Washed File Nov23 - 6/12/2023 - 01/11/2023.

december = Washed File Dec23 - 3/01/2024 - 1/12/2023.

January = Weekly File Jan24 -  10/01/2024 - 1/01/2024.

1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @transform99,

 

Result

dufoq3_0-1711349806227.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCk9Nzc6pVHDLzElV8MsvMzJW0lEy1Dc01DcyQGPH6mBXbUGSakNTkpQbGZGm3BK38sTijNQUVOVm+oZGhA13SU3GptoIv2pDY5KUGxmQptycKOVeiXlGJkAVxvoGYM+ZgFXD2OjBAjMcVTUhsw0NsBseCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, #"Date Created" = _t, #"Report Month" = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Date Created", type date}, {"Report Month", type date}}),
    Ad_NameHelper = Table.AddColumn(ChangedType, "NameHelper", each Text.BeforeDelimiter([Name], " "), type text),
    Ad_DateHelper = Table.AddColumn(Ad_NameHelper, "DateHelper", each Number.From(Date.ToText([Report Month], [Format="yyyyMM"])), Int64.Type),
    GroupedRows = Table.Group(Ad_DateHelper, {"DateHelper"}, {{"All", each
        [ a = Table.SelectRows(_, (x)=> x[NameHelper] = "Washed"),
          b = if Table.RowCount(a) > 0 then a else Table.FirstN(Table.Sort(_, {{"Date Created", Order.Descending}}), 1),
          c = Table.RemoveColumns(b, {"NameHelper", "DateHelper"})
        ][c], 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.

View solution in original post

3 REPLIES 3
dufoq3
Super User
Super User

Hi @transform99,

 

Result

dufoq3_0-1711349806227.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCk9Nzc6pVHDLzElV8MsvMzJW0lEy1Dc01DcyQGPH6mBXbUGSakNTkpQbGZGm3BK38sTijNQUVOVm+oZGhA13SU3GptoIv2pDY5KUGxmQptycKOVeiXlGJkAVxvoGYM+ZgFXD2OjBAjMcVTUhsw0NsBseCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, #"Date Created" = _t, #"Report Month" = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Date Created", type date}, {"Report Month", type date}}),
    Ad_NameHelper = Table.AddColumn(ChangedType, "NameHelper", each Text.BeforeDelimiter([Name], " "), type text),
    Ad_DateHelper = Table.AddColumn(Ad_NameHelper, "DateHelper", each Number.From(Date.ToText([Report Month], [Format="yyyyMM"])), Int64.Type),
    GroupedRows = Table.Group(Ad_DateHelper, {"DateHelper"}, {{"All", each
        [ a = Table.SelectRows(_, (x)=> x[NameHelper] = "Washed"),
          b = if Table.RowCount(a) > 0 then a else Table.FirstN(Table.Sort(_, {{"Date Created", Order.Descending}}), 1),
          c = Table.RemoveColumns(b, {"NameHelper", "DateHelper"})
        ][c], 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.

Thanks @dufoq3 this works and is more elegant than the solution I was looking at implementing.

You're welcome. Enjoy.


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

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