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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
MKE
Helper I
Helper I

Iterate over rows of a column as part of the RelativePath within a List.Generate() for API call

Hello,

 

I have a column with unique IDs.

I need to pass these IDs as part of the RelativePath in an API call using List.Generate() to return paginated results (offset,limit)

In OOP this would be something similar to: "for every ID, perform the GET request"

When I run the script below, the result is a list with ERROR as the record. If I expand the column, only the first ID returns the results and the rest ERRORs.

 

Here is my M script:

 

let
//This is the column with team Ids
    Source = 
        Json.Document(
            Web.Contents("https://api.dashpivot.com/", 
                    [
                        RelativePath = "companies/projects/teams/all",
                        Headers=[
                            Authorization="JWT xxxxxxx"
                            ]
                    ]
                )
                ),
    Source1 = Source{0},
    projects = Source1[projects],
    #"Converted to Table" = Table.FromList(projects, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"teams"}, {"Column1.teams"}),
    #"Expanded Column1.teams" = Table.ExpandListColumn(#"Expanded Column1", "Column1.teams"),
    #"Expanded Column1.teams1" = Table.ExpandRecordColumn(#"Expanded Column1.teams", "Column1.teams", {"id"}, {"id"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Column1.teams1",{{"id", type text}}),
    Startdate = Date.AddDays(
                    DateTime.Date(
                        DateTime.AddZone(DateTime.LocalNow(),10)), 
                        -#"Start Days"
                        ),
    Enddate = Date.AddDays(
                    DateTime.Date(
                        DateTime.AddZone(DateTime.LocalNow(),10)), 
                        #"End Days"
                        ),
//Adding the column with List.Generate to iterate over the IDs
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Data", each 
    
    List.Generate( () =>
       [
      
        Result=
        try Json.Document(
                Web.Contents(
                    "https://api.dashpivot.com/v1/",
                    [
                        RelativePath = "teams/"&[id]&"/photos?",
                        Query = [
                            limit="120",
                            offset=Number.ToText(OffsetValue),
                            startDate= Date.ToText(Startdate,"yyyy-MM-dd")&"T14:00:00.000Z",
                            endDate= Date.ToText(Enddate,"yyyy-MM-dd")&"T13:59:00.000Z"
                          ],        
                        Headers=[
                            Authorization="JWT xxxxx"
                        ]
                   ]
                )
            )otherwise try null, Offset = OffsetValue],
        each not List.IsEmpty([Result]),
        each 
        [
      
        Result=
        try Json.Document(
                Web.Contents(
                    "https://api.dashpivot.com/v1/",
                    [
                        RelativePath = "teams/"&[id]&"/photos?",
                        Query = [
                            limit="120",
                            offset=Number.ToText([Offset]+120),
                            startDate= Date.ToText(Startdate,"yyyy-MM-dd")&"T14:00:00.000Z",
                            endDate= Date.ToText(Enddate,"yyyy-MM-dd")&"T13:59:00.000Z"
                          ],        
                        Headers=[
                            Authorization="JWT xxxxxx"
                        ]
                   ]
                )
            )otherwise try null, Offset = [Offset]+120],
        each [Result]
        )
      )

 

 

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

Hi @MKE ,
unfortunately List.Generate can be a bit hard to debug. But it could be an ambiguity-problem here, as you are using it in a Table.AddColumn - operation. Try to replace the each by an explicite function definition ( (x)=> ) to get rid of the outer syntax sugar like so:

 

let
//This is the column with team Ids
    Source = 
        Json.Document(
            Web.Contents("https://api.dashpivot.com/", 
                    [
                        RelativePath = "companies/projects/teams/all",
                        Headers=[
                            Authorization="JWT xxxxxxx"
                            ]
                    ]
                )
                ),
    Source1 = Source{0},
    projects = Source1[projects],
    #"Converted to Table" = Table.FromList(projects, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"teams"}, {"Column1.teams"}),
    #"Expanded Column1.teams" = Table.ExpandListColumn(#"Expanded Column1", "Column1.teams"),
    #"Expanded Column1.teams1" = Table.ExpandRecordColumn(#"Expanded Column1.teams", "Column1.teams", {"id"}, {"id"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Column1.teams1",{{"id", type text}}),
    Startdate = Date.AddDays(
                    DateTime.Date(
                        DateTime.AddZone(DateTime.LocalNow(),10)), 
                        -#"Start Days"
                        ),
    Enddate = Date.AddDays(
                    DateTime.Date(
                        DateTime.AddZone(DateTime.LocalNow(),10)), 
                        #"End Days"
                        ),
//Adding the column with List.Generate to iterate over the IDs
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Data", (x)=>
    
    List.Generate( () =>
       [
      
        Result=
        try Json.Document(
                Web.Contents(
                    "https://api.dashpivot.com/v1/",
                    [
                        RelativePath = "teams/"&x[id]&"/photos?",
                        Query = [
                            limit="120",
                            offset=Number.ToText(OffsetValue),
                            startDate= Date.ToText(Startdate,"yyyy-MM-dd")&"T14:00:00.000Z",
                            endDate= Date.ToText(Enddate,"yyyy-MM-dd")&"T13:59:00.000Z"
                          ],        
                        Headers=[
                            Authorization="JWT xxxxx"
                        ]
                   ]
                )
            )otherwise null, Offset = OffsetValue],
        each not List.IsEmpty([Result]),
        each 
        [
      
        Result=
        try Json.Document(
                Web.Contents(
                    "https://api.dashpivot.com/v1/",
                    [
                        RelativePath = "teams/"&x[id]&"/photos?",
                        Query = [
                            limit="120",
                            offset=Number.ToText([Offset]+120),
                            startDate= Date.ToText(Startdate,"yyyy-MM-dd")&"T14:00:00.000Z",
                            endDate= Date.ToText(Enddate,"yyyy-MM-dd")&"T13:59:00.000Z"
                          ],        
                        Headers=[
                            Authorization="JWT xxxxxx"
                        ]
                   ]
                )
            )otherwise null, Offset = [Offset]+120],
        each [Result]
        )
      )

 

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

View solution in original post

7 REPLIES 7
ImkeF
Super User
Super User

Hi @MKE ,
unfortunately List.Generate can be a bit hard to debug. But it could be an ambiguity-problem here, as you are using it in a Table.AddColumn - operation. Try to replace the each by an explicite function definition ( (x)=> ) to get rid of the outer syntax sugar like so:

 

let
//This is the column with team Ids
    Source = 
        Json.Document(
            Web.Contents("https://api.dashpivot.com/", 
                    [
                        RelativePath = "companies/projects/teams/all",
                        Headers=[
                            Authorization="JWT xxxxxxx"
                            ]
                    ]
                )
                ),
    Source1 = Source{0},
    projects = Source1[projects],
    #"Converted to Table" = Table.FromList(projects, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"teams"}, {"Column1.teams"}),
    #"Expanded Column1.teams" = Table.ExpandListColumn(#"Expanded Column1", "Column1.teams"),
    #"Expanded Column1.teams1" = Table.ExpandRecordColumn(#"Expanded Column1.teams", "Column1.teams", {"id"}, {"id"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Column1.teams1",{{"id", type text}}),
    Startdate = Date.AddDays(
                    DateTime.Date(
                        DateTime.AddZone(DateTime.LocalNow(),10)), 
                        -#"Start Days"
                        ),
    Enddate = Date.AddDays(
                    DateTime.Date(
                        DateTime.AddZone(DateTime.LocalNow(),10)), 
                        #"End Days"
                        ),
//Adding the column with List.Generate to iterate over the IDs
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Data", (x)=>
    
    List.Generate( () =>
       [
      
        Result=
        try Json.Document(
                Web.Contents(
                    "https://api.dashpivot.com/v1/",
                    [
                        RelativePath = "teams/"&x[id]&"/photos?",
                        Query = [
                            limit="120",
                            offset=Number.ToText(OffsetValue),
                            startDate= Date.ToText(Startdate,"yyyy-MM-dd")&"T14:00:00.000Z",
                            endDate= Date.ToText(Enddate,"yyyy-MM-dd")&"T13:59:00.000Z"
                          ],        
                        Headers=[
                            Authorization="JWT xxxxx"
                        ]
                   ]
                )
            )otherwise null, Offset = OffsetValue],
        each not List.IsEmpty([Result]),
        each 
        [
      
        Result=
        try Json.Document(
                Web.Contents(
                    "https://api.dashpivot.com/v1/",
                    [
                        RelativePath = "teams/"&x[id]&"/photos?",
                        Query = [
                            limit="120",
                            offset=Number.ToText([Offset]+120),
                            startDate= Date.ToText(Startdate,"yyyy-MM-dd")&"T14:00:00.000Z",
                            endDate= Date.ToText(Enddate,"yyyy-MM-dd")&"T13:59:00.000Z"
                          ],        
                        Headers=[
                            Authorization="JWT xxxxxx"
                        ]
                   ]
                )
            )otherwise null, Offset = [Offset]+120],
        each [Result]
        )
      )

 

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

@ImkeF Thanks a lot. Definitely it was the ambiguity on the AddColumn. Looks like List.Generate() is one of the lazy ones. Your solution worked like magic! I appreciate

ImkeF
Super User
Super User

Hi @MKE ,
that's probably because your webcall returns a record and not a list. Could it be that you have to drill into the body-part instead?:

ImkeF_0-1664362080782.png

maybe: [Result][Body] ?

 

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

@ImkeF The webcall returns a list of records

ImkeF
Super User
Super User

Hi @MKE ,
what does the error-message say?

... just saw that you've used another "try" after the "otherwise". That's not correct syntax an must be deleted:

ImkeF_1-1664347912355.png

 

 

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

The try on otherwise is removed but still same error

Thanks for pointing that out @ImkeF , the error says 

Expression.Error: We cannot convert a value of type Record to type List.
Details:
    Value=
        HasError=FALSE
        Value=
    Type=[Type]

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Solution Authors