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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
joaovb96
Regular Visitor

Convert SQL to Power BI

Hello everybody.
I have this Select in SQL:

select sum(VL_SALDO) from RECEBER r
where not r.COD_SITUAC not in ('2', '7', '8') and coalesce(r.CH_ATIVO, 'T') = 'T'
and r.DT_VENCIM < current_date

This select is intended to return the total amount of overdue accounts receivable. The select SQL returns the value 3,326,100.87.
Does anyone have any idea how I could convert this select into a dax measure in Power BI to bring the same value?
I've found a few things in my searches, but so far none has really worked.
1 ACCEPTED SOLUTION

@joaovb96  DAX measure 

 

Measure =  --direct translation
CALCULATE (
    SUM ( 'COD_SITUAC'[VL_SALDO] ),
    FILTER (
        'COD_SITUAC',
        NOT 'COD_SITUAC'[COD_SITUAC]
            IN { 2, 7, 8 }
                && COALESCE ( 'COD_SITUAC'[CH_ATIVO], "T" ) = "T"
                && 'COD_SITUAC'[DT_VENCIM] < TODAY ()
    )
)

Measure = --optimized
CALCULATE (
    SUM ( 'COD_SITUAC'[VL_SALDO] ),
    FILTER (
        'COD_SITUAC',
        NOT 'COD_SITUAC'[COD_SITUAC]
            IN { 2, 7, 8 }
                && 'COD_SITUAC'[CH_ATIVO] = "T"
                && 'COD_SITUAC'[DT_VENCIM] < TODAY ()
    )
)

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

4 REPLIES 4
joaovb96
Regular Visitor

HI @Pragati11 @Greg_Deckler 
Thanks for answering. (sorry for my bad inglish)

Yes, all columns come from the same table

I tried to run as I was told earlier, but it didn't work, I tried changing the 'T' to True and later changing the column to True/False but it didn't work either.

I'll try to explain before then what this SELECT would be.
SELECT aims to show accounts receivable that are overdue.
select sum(VL_SALDO) from RECEIVE r
(this line will add up the amount to be received)

where not r.COD_SITUAC not in ('2', '7', '8') and coalesce(r.CH_ACTIVE, 'T') = 'T'
(This line will only filter the accounts receivable that do not have the code 2,7,8 and must also have the code T of TRUE

and r.DT_VENCIM < current_date
(and this is to say that the account receivable date has to be less than the current date, therefore it is late)

Captura de tela 2022-01-05 135538.png



@joaovb96  DAX measure 

 

Measure =  --direct translation
CALCULATE (
    SUM ( 'COD_SITUAC'[VL_SALDO] ),
    FILTER (
        'COD_SITUAC',
        NOT 'COD_SITUAC'[COD_SITUAC]
            IN { 2, 7, 8 }
                && COALESCE ( 'COD_SITUAC'[CH_ATIVO], "T" ) = "T"
                && 'COD_SITUAC'[DT_VENCIM] < TODAY ()
    )
)

Measure = --optimized
CALCULATE (
    SUM ( 'COD_SITUAC'[VL_SALDO] ),
    FILTER (
        'COD_SITUAC',
        NOT 'COD_SITUAC'[COD_SITUAC]
            IN { 2, 7, 8 }
                && 'COD_SITUAC'[CH_ATIVO] = "T"
                && 'COD_SITUAC'[DT_VENCIM] < TODAY ()
    )
)

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Pragati11
Super User
Super User

HI @joaovb96 ,

 

Can you share some sample data here please?

 

I am assuming all columns in above SQL query are coming from same table.

You can easily convert this SQL query in DAX. Something like below:

totalAmount =
VAR defVal = "T"
VAR mainVal =
    SELECTEDVALUE ( yourtablename[CH_ATIVO] )
RETURN
    CALCULATE (
        SUM ( yourtablename[VL_SALDO] ),
        FILTER (
            yourtablename,
            NOT ( yourtablename[COD_SITUAC] )IN { '2', '7', '8' } 
            && COALESCE ( mainVal, defVal )
            && yourtablename[DT_VENCIM] < TODAY ()
        )
    )

 The above DAX is just written based on looking at your SQL query. I can't test it as I don't have access to your data.

 

Share some sample data or pbix file by removing any sensitive information so that I can test this DAX.

 

Thanks,

Pragati

Best Regards,

Pragati Jain


MVP logo


LinkedIn | Twitter | Blog YouTube 

Did I answer your question? Mark my post as a solution! This will help others on the forum!

Appreciate your Kudos!!

Proud to be a Super User!!

Greg_Deckler
Community Champion
Community Champion

@joaovb96 Going to be something along the lines of:

Measure =
  CALCULATE(SUM('RECEBER'[VL_SALDO]),FILTER('RECEBER', [COD_SITUAC] NOT IN {2,7,8} && [DT_VENCIM] < TODAY() && [CH_ATIVO] = "T")

Usually better if you just post sample data and expected output. Sorry, having trouble following, can you post sample data as text and expected output?
Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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