如何循环遍历 M 查询中的列表?

How to loop through a list in a M query?

我在 PowerQuery 编辑器中有一个查询,它带来了 Azure DevOps 组织中的所有项目:

let
   Source = OData.Feed("https://analytics.dev.azure.com/someorg/_odata/v3.0-preview/Projects", null, [Implementation="2.0", OmitValues=ODataOmitValues.Nulls, ODataVersion=4]),
    ProjectName = Source[ProjectName]
in
    ProjectName

另外,我有这个 OData 查询,它可以返回单个项目的所有任务:

let
   Source = OData.Feed ("https://analytics.dev.azure.com/someorg/{project}/_odata/v3.0-preview/WorkItems?"
        &"$filter=WorkItemType eq 'User Story' "
            &"and State ne 'Closed' "
            &"and startswith(Area/AreaPath,'{areapath}') "
        &"&$select=WorkItemId,Title,WorkItemType,State,Priority,Severity,TagNames,AreaSK "
        &"&$expand=AssignedTo($select=UserName),Iteration($select=IterationPath),Area($select=AreaPath), "
                &"Links( "
                    &"$filter=LinkTypeName eq 'Related' "
                        &"and TargetWorkItem/WorkItemType eq 'User Story'; "
                    &"$select=LinkTypeName; "
                    &"$expand=TargetWorkItem($select=WorkItemType,WorkItemId,Title,State) "
                &") "
    ,null, [Implementation="2.0",OmitValues = ODataOmitValues.Nulls,ODataVersion = 4]) 
in
    Source

我需要 运行 第一个列表中每个项目的第二个查询一次,并将它们追加到单个 table 中,为每个元素替换 {project}。这在 PowerQuery 中可能吗?

提前致谢;

  1. 创建一个自定义函数,结果将是您的 table

在你的情况下是这样的:

查询名称是 GetDataForProject

(project) =>
let
   Source = OData.Feed ("https://analytics.dev.azure.com/someorg/{" & project & "}/_odata/v3.0-preview/WorkItems?"
        &"$filter=WorkItemType eq 'User Story' "
            &"and State ne 'Closed' "
            &"and startswith(Area/AreaPath,'{areapath}') "
        &"&$select=WorkItemId,Title,WorkItemType,State,Priority,Severity,TagNames,AreaSK "
        &"&$expand=AssignedTo($select=UserName),Iteration($select=IterationPath),Area($select=AreaPath), "
                &"Links( "
                    &"$filter=LinkTypeName eq 'Related' "
                        &"and TargetWorkItem/WorkItemType eq 'User Story'; "
                    &"$select=LinkTypeName; "
                    &"$expand=TargetWorkItem($select=WorkItemType,WorkItemId,Title,State) "
                &") "
    ,null, [Implementation="2.0",OmitValues = ODataOmitValues.Nulls,ODataVersion = 4]) 
in
    Source
  1. 将您的列表转换为 table 并在您使用创建的函数的位置添加新列,例如:

     let
          Source = Table.FromList(YourListOfProjects)
         ,InvokedFunction = Table.AddColumn(Source, "GetDataForProject", each GetDataForProject([Column1]))
     in
         InvokedFunction
    
  2. 然后您可以在新创建的列中展开 tables:

结果你得到附加 tables.