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
hworrall
Frequent Visitor

Complex query

Hi there,

 

I am new to Power BI and am trying to write a query where I need to subtract the result of one query from the result of a second query.  Here is what I have in SQL/English.  There are also two different date columns, it is not a typo.  I am not sure how to do it at all, but also would like to find the most efficient way to do it.

 

Any help is appreciated 🙂

 

Query 1:

Sum TotalCharge where:

 

SELECT Wdate, Wpdate, Whours, Wfee, Wexp, wFee+wExp+wSurchg as TotalCharge, Wdstamp, Wipdate, Windicator
FROM WIP
WHERE Wdate < '2018-05-01' AND Wipdate > '2018-04-30' AND (Windicator = 'w' or Windicator = 'h')

 

MINUS

 

Query 2:

Sum TotalCharge where:

SELECT Wdate, Wpdate, Whours, Wfee, Wexp, wFee+wExp+wSurchg as TotalCharge, Wdstamp, Wipdate, Windicator
FROM WIP

WHERE Wdate > '2018-04-30' AND Wipdate < '2018-05-01' AND (Windicator = 'w' or Windicator = 'h')

 

Thank you!

1 REPLY 1
ImkeF
Community Champion
Community Champion

Hi @hworrall ,

please paste this code into the advanced editor and follow the steps:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUAyIjA0MLJR0wxxzGKQdiQwOlWB18ijKA2IiQokQgNkZTZInNOhNirDMlxjozNEXmUEVwDsg6c6CiWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Wdate = _t, Wipdate = _t, Windicator = _t, TotalCharge = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Wdate", type date}, {"Wipdate", type date}, {"Windicator", type text}, {"TotalCharge", Int64.Type}}),
    Filter1 = Table.SelectRows(#"Changed Type", each [Wdate] < #date(2019, 5, 1) and [Wipdate] > #date(2018, 4, 30) and ( [Windicator] = "w" or [Windicator] = "h" ) ),
    Query1 = List.Sum(Filter1[TotalCharge]),
    Filter2 = Table.SelectRows(#"Changed Type", each [Wdate] > #date(2018, 4, 30) and [Wipdate] < #date(2019, 5, 1) and ( [Windicator] = "w" or [Windicator] = "h" ) ),
    Query2 = List.Sum(Filter2[TotalCharge]),
    Result = Query1 + Query2
in
    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

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!

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.