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
Ricardo77
Helper II
Helper II

How to get the value from the same table but with different context ? ...

Hello, i have an architectural doubt on DAX, both: how I should I do to get the value, while not forgetting performance and BPs... 

 

Basically i pick 2 codemoments from a matrix, and i have to calculate some kind of variation like

(somevalue from factual in codemoment_t_1 / somevalue2 from dimension in codemoment_t_1) *
(somevalue2 from dimension in codemoment_tsomevalue2 from dimension in codemoment_t_1)

 

My only problem right now is the one highlighted in red: i'm trying to get the value but i don't know how to do it.

Conceptually, should be something like ...

(CALCULATE(RELATED(DIMENSION[SOMEVALUE2]); FACTUAL[CODEMOMENT] = CODEMOMENT_T)

... or like i'm doing there, using the ID and making the relation temporarily n to 1 ...

 

RESULT  = 
VAR SELECTED_2_MOMENTS = ALLSELECTED(FACTUAL[CODEMOMENT])
VAR CODEMOMENT_T =   CALCULATE(MAX(FACTUAL[CODEMOMENT]); FACTUAL[CODEMOMENT] IN SELECTED_2_MOMENTS) 
VAR CODEMOMENT_T_1 = CALCULATE(MIN(FACTUAL[CODEMOMENT]); FACTUAL[CODEMOMENT] IN SELECTED_2_MOMENTS)

RETURN IF (CODEMOMENT_T <> CODEMOMENT_T_1; 
            CALCULATE (
                SUMX(
                    FACTUAL;        
                    DIVIDE(FACTUAL[SOMEVALUE]; RELATED(DIMENSION[SOMEVALUE2])) 
                    *
                     -- calculation problem next line 					
                    (CALCULATE(MAX(DIMENSION[SOMEVALUE2]); FACTUAL[CODEMOMENT] = CODEMOMENT_T; CROSSFILTER(FACTUAL[ID_CONCATENATED_ENTRY]; DIMENSION[ID_CONCATENATED_ENTRY]; OneWay))
					 -- calculation problem previous line
                                - RELATED(DIMENSION[SOMEVALUE2]))  
                    ;
                    FACTUAL[CODEMOMENT] = CODEMOMENT_T_1)
           )            

 

This is the model associated:

model.png

 

Can someone help me out? Thanks in advance.

3 REPLIES 3
Greg_Deckler
Super User
Super User

@Ricardo77 -

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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Hello @Greg_Deckler ,

 

Thanks, let me update the doubt:
So the measure is this and my problem is getting to a different context in that line between comments

 

 

 

RESULT  = 
VAR SELECTED_2_MOMENTS = ALLSELECTED(FACTUAL[CODEMOMENT]) -- this will pick 11200 and 13500
VAR CODEMOMENT_T =   CALCULATE(MAX(FACTUAL[CODEMOMENT]); FACTUAL[CODEMOMENT] IN SELECTED_2_MOMENTS) -- so, 13500
VAR CODEMOMENT_T_1 = CALCULATE(MIN(FACTUAL[CODEMOMENT]); FACTUAL[CODEMOMENT] IN SELECTED_2_MOMENTS) -- 11200

RETURN IF (CODEMOMENT_T <> CODEMOMENT_T_1; 
            CALCULATE (
                SUMX(
                    FACTUAL;        
                    DIVIDE(FACTUAL[SOMEVALUE]; RELATED(DIMENSION[SOMEVALUE2])) 
                    *
                     -- calculation problem next line, need the value from 13500 					
                    (CALCULATE(MAX(DIMENSION[SOMEVALUE2]); FACTUAL[CODEMOMENT] = CODEMOMENT_T; CROSSFILTER(FACTUAL[ID_CONCATENATED_ENTRY]; DIMENSION[ID_CONCATENATED_ENTRY]; OneWay))
					 -- calculation problem previous line
                                - RELATED(DIMENSION[SOMEVALUE2]))  
                    ;
                    FACTUAL[CODEMOMENT] = CODEMOMENT_T_1)-- so, all SUMX is running over 11200, EXCEPT that value from 13500
           )       

 

 

 

 

Conceptually, should be something like this, where codemoment_t = 13500; that's why i tried to play with crossfilter direction and RELATED to pick the value from the dimension on a different context. The idea is, for the same context, just change CODEMOMENT from 11200 for 13500

 

 

 

(CALCULATE(RELATED(DIMENSION[SOMEVALUE2]); FACTUAL[CODEMOMENT] = CODEMOMENT_T)

 

 

ort.png

 

Factual

ID_CONCATENATED_ENTRYCODEMOMENTTYPEOFENTRIESOMEVALUE
ABC1120011200S806663
ABC1120011200S264560.44
ABC1120011200S67574694.67
ABC1120011200S1148038
ABC1120011200S364358905

 

Dimension

ID_CONCATENATED_ENTRYCODEMOMENTSOMEVALUE2
ABC11200112000.00003751
ABC11200112000.00324386
ABC11200112000.007120278
ABC11200112000.00838303
ABC11200112000.00949994
ABC13500135000.00003989
ABC13500135000.00324
ABC13500135000.0071
ABC13500135000.008
ABC13500135000.008

 

Dax calculation should do, for each line ….

( 806663 / 0.00003751) * (0.00003989 - 0.00003751)

The final result should be the sum of the last column of this table:

SOMEVALUE  in 12200 | SOMEVALUE2 in 12200 | SOMEVALUE in 13500 | SOMEVALUE is again from 12200

2020-09-10_10h31_47.png

 

Thanks in advance for your help!

@Ricardo77 Wondering if maybe you need to use TREATAS but I can't really tell because I can't mock this up without data.



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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

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.