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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Rune_
Frequent Visitor

Sum of values in one query based on filter from another Query

I have one query called

JobTasks with these columns

 

JobNo | JobTaskFrom | JobTaskTo

10100 | 10                | 20

 

I would like to add a Column called "Amount" to this Query that adds values from a query called

 

JobLedger

JobNo | JobTask | Amount

10100 | 11         | 50

10100 | 15         | 60

 

The filters for adding Amount is from the query JobTasks

JobTask >= then JobTaskFrom and <= then JobTaskTo

How do I write this function?

1 ACCEPTED SOLUTION
Smauro
Solution Sage
Solution Sage

Hi @Rune_ 

It's certainly not the quickest way, but you can try this:

 

 

 

    Amount = Table.AddColumn(PreviousStep, "Amount", each let no = [JobNo], from = [JobTaskFrom], to = [JobTaskTo] in List.Sum(Table.SelectRows(JobLedger, each [JobNo]=no and [JobTask]>=from and [JobTask]<=to)[Amount]), type number)

 

 

 

Where PreviousStep is the name of your last step.

Cheers


Edit:
This should be a bit quicker:

 

    #"Merge Queries" = Table.NestedJoin(PreviousStep, {"JobNo"}, Table.SelectColumns(JobLedger, {"JobNo", "JobTask", "Amount"}), {"JobNo"}, "JobLedger", JoinKind.LeftOuter),
    #"Added Amount" = Table.AddColumn(#"Merge Queries", "Amount", each let from = [JobTaskFrom], to = [JobTaskTo] in List.Sum(Table.SelectRows([JobLedger], each _[JobTask]>=from and _[JobTask]<=to)[Amount]), type number),
    #"Remove JobLedger" = Table.RemoveColumns(#"Added Amount", {"JobLedger"})

 

 




Spyros Mavroforos

Data Consultant


Find me on LinkedIn or drop me an email

eSpyros

View solution in original post

6 REPLIES 6
mahoneypat
Microsoft Employee
Microsoft Employee

Here is another approach to consider:

 

  • merge the second query into the first one on JobNo, calling the new colum AmtTable (leave it in Table form)
  • add a custom column with this function: = List.Sum([AmtTable][Amount])

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Smauro
Solution Sage
Solution Sage

Hi @Rune_ 

It's certainly not the quickest way, but you can try this:

 

 

 

    Amount = Table.AddColumn(PreviousStep, "Amount", each let no = [JobNo], from = [JobTaskFrom], to = [JobTaskTo] in List.Sum(Table.SelectRows(JobLedger, each [JobNo]=no and [JobTask]>=from and [JobTask]<=to)[Amount]), type number)

 

 

 

Where PreviousStep is the name of your last step.

Cheers


Edit:
This should be a bit quicker:

 

    #"Merge Queries" = Table.NestedJoin(PreviousStep, {"JobNo"}, Table.SelectColumns(JobLedger, {"JobNo", "JobTask", "Amount"}), {"JobNo"}, "JobLedger", JoinKind.LeftOuter),
    #"Added Amount" = Table.AddColumn(#"Merge Queries", "Amount", each let from = [JobTaskFrom], to = [JobTaskTo] in List.Sum(Table.SelectRows([JobLedger], each _[JobTask]>=from and _[JobTask]<=to)[Amount]), type number),
    #"Remove JobLedger" = Table.RemoveColumns(#"Added Amount", {"JobLedger"})

 

 




Spyros Mavroforos

Data Consultant


Find me on LinkedIn or drop me an email

eSpyros

Rune_
Frequent Visitor

Hi @Smauro 

 

Is there any way I can add a filter to this function

Amount = Table.AddColumn(PreviousStep, "Amount", each let no = [JobNo], from = [JobTaskFrom], to = [JobTaskTo] in List.Sum(Table.SelectRows(JobLedger, each [JobNo]=no and [JobTask]>=from and [JobTask]<=to)[Amount]), type number)

 

I have a column with dates and I want a filter on the dates that shows all dates < then EndOfMonth last month (example todays date is 23.06 then I would like it to show dates < then 31.05)  

Hi @Rune_ 

 

Assuming that your [Date] column is in 'JobLedger' then this should work:

Amount = Table.AddColumn(PreviousStep, "Amount", each let no = [JobNo], from = [JobTaskFrom], to = [JobTaskTo], d = Date.StartOfMonth(DateTime.Date(DateTime.LocalNow())) in List.Sum(Table.SelectRows(JobLedger, each [Date]<d and [JobNo]=no and [JobTask]>=from and [JobTask]<=to)[Amount]), type number)

 




Spyros Mavroforos

Data Consultant


Find me on LinkedIn or drop me an email

eSpyros

Rune_
Frequent Visitor

Thank you. 

This worked perfectly 😀

Rune_
Frequent Visitor

Hi @Smauro , 

 

Thank you for your quick reply. 

I used your first solutions "Amount = Table.AddColumn..." and it worked perfectly. 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.