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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
linros
Frequent Visitor

Relate one column whith another colunm inside a subselect

Hi everyone!

 

I need to convert this query in SQL to DAX, but I need to relate one column whith another colunm inside a subselect.

I tried different functions but I did not succeed.

 

SELECT a.Year
,COUNT(a.ID_Prop)
,(SELECT top 1 COUNT(b.ID_Prop) FROM Prop b where a.Year = (b.Year - 1) group by Ano) - COUNT(a.ID_Prop)
FROM Prop a
where Year is not null
group by Year
order by Year

 

Someone can help me with that?

1 ACCEPTED SOLUTION
linros
Frequent Visitor

I created a new Column and use that DAX:

 

Variancy =
        VAR lastYear = 'Basis 1'[Year] - 1
        VAR actualYear = 'Basis 1'[Year]
Return
        COUNTROWS(FILTER('Basis 1';'Basis 1'[Year] = actualYear))

        - COUNTROWS(FILTER('Basis 1';'Basis 1'[Year] = lastYear))

 

That resolved my problem!

 

Tk's dedelman_clng

=D

View solution in original post

4 REPLIES 4
linros
Frequent Visitor

I created a new Column and use that DAX:

 

Variancy =
        VAR lastYear = 'Basis 1'[Year] - 1
        VAR actualYear = 'Basis 1'[Year]
Return
        COUNTROWS(FILTER('Basis 1';'Basis 1'[Year] = actualYear))

        - COUNTROWS(FILTER('Basis 1';'Basis 1'[Year] = lastYear))

 

That resolved my problem!

 

Tk's dedelman_clng

=D

dedelman_clng
Community Champion
Community Champion

Can you explain (not in SQL code) what value you are trying to find?  SQL code doesn't always translate easily to DAX, but knowing the requirement would get you pointed in the right direction.

I need to calculate the variancy of the proposals by the year for the waterfall chart. I have a table with the proposals IDs and the year of the proposal. so, I need to calculate the diference of the number of proposals of the previous year and the number of proposals of the year that the column make reference. That`s possible?

You will want to add a date table to your data model, so you can make use of time intelligence functions.  Once you have that, and join it to the rest of your model, you can use the SAMEPERIODLASTYEAR() function.  For example

 

CountVariance = 
       CALCULATE(
              COUNT('Proposal'[ID])
       ) - 
       CALCULATE( 
            COUNT('Proposal'[ID]),
            SAMEPERIODLASTYEAR('Proposal'[Date])
       )

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.