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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
dragon2
Frequent Visitor

Change from PY DAX code is not correct for some records

Hello,

 

My measure for "Change from PY" is not correct for some records for 2021 and 2022. For example: Apple, Full, 2022, Change should be -9. Pear, Part, 2021, Change should be -1. Pear, Full, 2022, Change should be -10.

 

What is wrong with my current DAX code? I created a Calendar table and connected 'Calendar'[Year] to 'Sheet1'[Year].

 

Capture.JPG

PY-DAX-is-wrong.pbix

Calendar = CALENDAR(DATE(2020,1,1),DATE(2022,12,31))
Year = YEAR('Calendar'[Date])
newCount = CALCULATE(SUM(Sheet1[Count]))+0
Change from PY = 
VAR
    vCY = MAX('Calendar'[Year])
VAR vPYAmount =
    CALCULATE([newCount],'Calendar'[Year]=vCY - 1)
VAR vCYAmount =
    CALCULATE([newCount])
VAR Result =
    vCYAmount - vPYAmount
RETURN Result
GroupTypeMetricCountYear
AAppleFull52020
AAppleFull92021
AApplePart22022
BPearPart12020
BPearFull102021
1 ACCEPTED SOLUTION
gmsamborn
Super User
Super User

Hi @dragon2 

 

The DAX isn't the issue.. 

The problem is with your many-to-many relationship on [Year].  

Instead I created a caculated column on your Sheet1 table:    Date = DATE( [Year], 1, 1 )

Use that in your relationship with your date table in a simple one-to-many relationship.
 
Let me know if you have any questions.
 
 

 



Proud to be a Super User!

daxformatter.com makes life EASIER!

View solution in original post

4 REPLIES 4
Ashish_Excel
Super User
Super User

Hi,

Download the PBI file from here.

Hope this helps.

Ashish_Excel_0-1747537079051.png

 

Elena_Kalina
Solution Sage
Solution Sage

Hi, @gmsamborn 

Using your sample data as a reference, I created a simple table. Then, to generate the column you need, I added a calculated column using the following formula:

Change from PY =
VAR CY_Type = 'Fruit'[Type]
VAR CY = 'Fruit'[Year]
VAR CY_Metric = 'Fruit'[Metric]
VAR CY_Amount ='Fruit'[NewCount]
VAR PY_Amount_ =
    CALCULATE(
        SUM('Fruit'[NewCount]),
        FILTER(
            ALL('Fruit'),
            'Fruit'[Type] = CY_Type && 'Fruit'[Metric] = CY_Metric &&
            'Fruit'[Year] = CY - 1
        )
    )
VAR PY_amount = IF(ISBLANK(PY_Amount_),
                   0,
                   PY_Amount_
                )
RETURN
CY_Amount - PY_amount
Elena_Kalina_1-1747419466541.png

I believe this should give you exactly what you were looking for.

 

If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos" – I’d truly appreciate it! 

Thank you.

 

gmsamborn
Super User
Super User

Hi @dragon2 

 

The DAX isn't the issue.. 

The problem is with your many-to-many relationship on [Year].  

Instead I created a caculated column on your Sheet1 table:    Date = DATE( [Year], 1, 1 )

Use that in your relationship with your date table in a simple one-to-many relationship.
 
Let me know if you have any questions.
 
 

 



Proud to be a Super User!

daxformatter.com makes life EASIER!

Thank you so much.

 

For some reason, with my acutal data, this was not working. So, instead of making the Calendar table that I shared originally, I made a new table like this:

newCalendar = DISTINCT(Sheet1[Year])

 

Then I made a 1-to-many relationship on this table and sheet1[Year]. It worked perfectly after that.

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors