Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Grouping information from multiple tables

Hi All,

 

I have an problem with querying data that comes from multiple SQL tables and outputs to a format such as Excel, so that I can  identify gaps in data for end users and provide them a tool where they can record any data that needs to be completed.

 

My example tables are as follows:

jobplan table

jpnumdescriptiondurationstatusjobplanid
JP1000JP Description 18Active2101

 

jobtask table

jptaskdescriptionjpnumjobplanid
10JP task 1JP10002101
20JP task 2JP10002101
10JP task 1JP10001999
20JP task 2JP10001999

 

joblabour table

craftquantitylaborhrsjptaskjobplanidjpnum
ELEC28 2101JP1000
EXT-ELEC14202101JP1000
MECH28 2101JP1000
TA412102101JP1000
EXT-MECH14102101JP1000

 

jobmaterial table

itemnumdescriptionquantityjobplanidjpnumjptask
122GASKET12101JP100020
543Nut22101JP100020
67895Bolt12101JP100020
123GASKET11999JP100020

 

 

The desired output needs to be able to show a table that combines all this data, without duplicating all the rows for all the variables and group the common data to make it easier to read. Apologies if this isn't quite the righgt forum for this but I was hoping to do this in power query so that i can adjust the tool dynamically as data is updated. Happy to have a discussion in another forum, or offline from here if there is someone who has a solution, btu this isn't quite where it fits.

 

Thanks in advance, Craig

 

Desired output:

jpnumdescriptiondurationstatusjobplanidjptaskdescriptionjpnumjobplanidcraftquantitylaborhrsjptaskjobplanidjpnumitemnumdescriptionquantityjobplanidjpnumjptask
JP1000JP Description 18Active210110JP task 1JP10002101TA412102101JP1000      
         EXT MECH14102101JP1000      
     20JP task 2JP10002101EXT-ELEC14202101JP1000122GASKET12101JP100020
               543Nut22101JP100020
               67895Bolt12101JP100020
         ELEC28 2101JP1000      
         MECH28 2101JP1000      

 

 

  • = List.Accumulate({"JobPlan","JobTask","JobLabour","JobMaterial"},"",(x,y)=>let a=Record.Field(#shared,y),b=List.Intersect({{"jpnum","jobplanid","jptask"},Table.ColumnNames(a),Table.ColumnNames(x)}),c=Table.ColumnNames(a),d=List.Transform(c,each if List.Contains(Table.ColumnNames(x),_) then y&"-"&_ else _),e=Table.NestedJoin(x,b,a,b,"n",JoinKind.FullOuter) in if x="" then a else Table.ExpandTableColumn(e,"n",c,d))

     

2 Replies

  • wdx223_Daniel's avatar
    wdx223_Daniel
    Community Champion

    = List.Accumulate({"JobPlan","JobTask","JobLabour","JobMaterial"},"",(x,y)=>let a=Record.Field(#shared,y),b=List.Intersect({{"jpnum","jobplanid","jptask"},Table.ColumnNames(a),Table.ColumnNames(x)}),c=Table.ColumnNames(a),d=List.Transform(c,each if List.Contains(Table.ColumnNames(x),_) then y&"-"&_ else _),e=Table.NestedJoin(x,b,a,b,"n",JoinKind.FullOuter) in if x="" then a else Table.ExpandTableColumn(e,"n",c,d))

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Daniel, thanks for having a crack, this isn't quitre what I was after , but i'll keep working through it and see what I can get.