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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Halmar
Regular Visitor

Loop over a table to generate open files

Hi all,

 

I have a table with end of the month dates.

Date

31-01-22
28-02-22
31-03-22
30-04-22

 

And I have a table with files and their Startdate and Closedate.

FileIDStartdateClosedate
100101-12-2101-03-22
200215-02-2202-04-22

 

I want to create a table with per end of the month date the 'open' files:

DateOpen fileID
31-01-221001
28-02-221001
28-02-222002
31-03-222002

 

I want to do this with a loop.

- Looping over de days

- Filtering the Filetable and check wich Files are open on that date

- Concatenate the results of every loop

 

Thanks for your help!

 

Halmar

1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

See the working here for Table2 (it refers to first table as Table1) - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcnBDQAgCATBXu5NwnFqNYT+2xCDv81sJoIMGEIeLur1mhTKEiLV1vsMGrbr/7o=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [FileID = _t, Startdate = _t, Closedate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"FileID", Int64.Type}, {"Startdate", type date}, {"Closedate", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Open fileID", each Table.SelectRows(Table1, (x)=> x[Date]>=[Startdate] and x[Date]<=[Closedate])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Startdate", "Closedate"}),
    #"Expanded Open fileID" = Table.ExpandTableColumn(#"Removed Columns", "Open fileID", {"Date"}, {"Date"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded Open fileID",{"Date", "FileID"})
in
    #"Reordered Columns"

 If you need code for Table1 as well

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ3NtQ3MjAyUorViVYy0jeyQPCMUeRM9I0NoLxYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}})
in
    #"Changed Type"

View solution in original post

1 REPLY 1
Vijay_A_Verma
Super User
Super User

See the working here for Table2 (it refers to first table as Table1) - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcnBDQAgCATBXu5NwnFqNYT+2xCDv81sJoIMGEIeLur1mhTKEiLV1vsMGrbr/7o=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [FileID = _t, Startdate = _t, Closedate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"FileID", Int64.Type}, {"Startdate", type date}, {"Closedate", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Open fileID", each Table.SelectRows(Table1, (x)=> x[Date]>=[Startdate] and x[Date]<=[Closedate])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Startdate", "Closedate"}),
    #"Expanded Open fileID" = Table.ExpandTableColumn(#"Removed Columns", "Open fileID", {"Date"}, {"Date"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded Open fileID",{"Date", "FileID"})
in
    #"Reordered Columns"

 If you need code for Table1 as well

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ3NtQ3MjAyUorViVYy0jeyQPCMUeRM9I0NoLxYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}})
in
    #"Changed Type"

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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