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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
nuance
Frequent Visitor

Unable to Generate Table by Looping Pages from API while Checking Table Contents as Condition

Currently I am using the following code to loop through pages until an empty page is reached by using Table.RowCount([Func]) >= 1.

Now I wanna change the code such that the looping stops when the records reached certain "CreatedDate" to retrieve only the most updated pages of records. I am so stuck in setting up the looping condition as nothing seems working, any idea? many thanks!

 

 

let
    GetAllData = (Project as any) => let
    GetData = (Page as number) => 
    let
        Source = Json.Document(Web.Contents(CompanyURL , [RelativePath =  "/" & Project & "/api/v2/formitems?key=" & APIKey & "&pagesize=5000&page=" & Number.ToText(Page)])), 
        #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
    in
        #"Converted to Table",
    
    #"List" = 
    List.Generate( ()=> 
    [Page=0, Func=null],
     each [Page] = 0 or Table.RowCount([Func]) >= 1,
     each [Page =_[Page]+1,
     Func=GetData([Page]+1)],
     each _[Func]),
        #"Converted to Table" = Table.FromList(List, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Removed Top Rows" = Table.Skip(#"Converted to Table",1),
        #"Expanded Column1" = Table.ExpandTableColumn(#"Removed Top Rows", "Column1", {"Column1"}, {"Column1.1"}),
        #"Expanded Column1.1" = Table.ExpandRecordColumn(#"Expanded Column1", "Column1.1", {"ID", "CreatedDate"}, {"ID", "CreatedDate"}),
      
    in
        #"Expanded Column1.1",

Project = () =>
let
    Source = Json.Document(Web.Contents(CompanyURL, [RelativePath = "/Admin/api/projects?key=" & APIKey])),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"URL", "Finished"}, {"URL", "Finished"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded Column1", each ([Finished] = false)),
    #"Inserted Text After Delimiter" = Table.AddColumn(#"Filtered Rows", "Text After Delimiter", each Text.AfterDelimiter([URL], "/"), type text),
    #"Renamed Columns" = Table.RenameColumns(#"Inserted Text After Delimiter",{{"Text After Delimiter", "Project"}, {"URL", "FullURL"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Finished"})
in
    #"Removed Columns",


Source = Project (),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each GetAllData([Project])),

in
    #"Added Custom"

 

 

1 REPLY 1
ImkeF
Community Champion
Community Champion

Hi @nuance ,
how would you know the most updated data before entering the loop?
Or: If you need to determine that while looping: Wouldn't you have to run through the whole loop until finding out the most recent one?

So: Please share the logic how the most recent date can be determined.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.