Forum Discussion

terdudov2's avatar
terdudov2
Frequent Visitor
2 years ago

Measure to relate data from 2 tables

Hello, 

 

I hope someone could advice me in my case. I have a table with buy/sell of shares and another table with market prices.

I need to create a table (from source1 data), where I have share name, nr of pieces and latest market price, and a filter by months. I need the market price to change if I filter different months. So if I filter month 4, I need the market price for CEZ to show value 503 etc. Can someone advise me with formula for the market price measure?

 

thank you!

 

Source 1:

DateMonthSharePieces
01.03.20233CEZ AS300
06.05.20235ANHEUSER400
08.09.20239BAYER AG-REG200

 Source 2:

ShareMonthMarket price
CEZ AS1500
CEZ AS2501
CEZ AS3502
CEZ AS4503
CEZ AS5504
CEZ AS6505
CEZ AS7506
CEZ AS8507
CEZ AS9508
CEZ AS10509
CEZ AS11510
CEZ AS12511
ANHEUSER134,5
ANHEUSER234,6
ANHEUSER334,7
ANHEUSER434,8
ANHEUSER534,9
ANHEUSER635,0
ANHEUSER735,1
ANHEUSER835,2
ANHEUSER935,3
ANHEUSER1035,4
ANHEUSER1135,5
ANHEUSER1235,6
BAYER AG-REG121,0
BAYER AG-REG222,0
BAYER AG-REG323,0
BAYER AG-REG424,0
BAYER AG-REG525,0
BAYER AG-REG626,0
BAYER AG-REG727,0
BAYER AG-REG828,0
BAYER AG-REG929,0
BAYER AG-REG1030,0
BAYER AG-REG1131,0
BAYER AG-REG1232,0

5 Replies

  • saud968's avatar
    saud968
    Icon for Memorable Member rankMemorable Member

    To achieve this, you can create a measure in your Source 1 table that looks up the latest market price for each share based on the selected month. You can use the following DAX (Data Analysis Expressions) formula in Power BI or Excel (assuming you have a relationship between Source 1 and Source 2 tables on the "Share" and "Month" columns):


    Market Price =
    VAR SelectedMonth = SELECTEDVALUE('Source 1'[Month])
    RETURN
    CALCULATE(
    MAX('Source 2'[Market price]),
    FILTER(
    'Source 2',
    'Source 2'[Share] = 'Source 1'[Share] &&
    'Source 2'[Month] = SelectedMonth
    )
    )
    This measure uses the CALCULATE function to evaluate the maximum (latest) market price for the selected share and month. The FILTER function filters the Source 2 table based on the selected share and month in Source 1.

    Remember to replace 'Source 1' and 'Source 2' with your actual table names. Once you create this measure, you can use it in your Source 1 table, and it should dynamically update the market price based on the selected month.

    Note: Ensure that there is a relationship between the "Share" columns in both tables for this measure to work correctly. If there isn't one, you may need to create a relationship between the "Share" columns in Source 1 and Source 2 tables.

    Best Regards
    Saud Ansari
    If this post helps, please Accept it as a Solution to help other members find it. I appreciate your Kudos!

  • terdudov2's avatar
    terdudov2
    Frequent Visitor

    Hello, 

    the formula gives me error (the value that gives error is the name of the share).

     

    Also I should mention that i work with Calendar(date) and the "month" filter is based on "Calendar(month) value. Also it gets more complicated, as when I do not filter any month, I need the last known value to be shown.

    Also the relationship between share from 2 tables is problematic, since it is m:n relationship

    If it helps with the formula, It does not need to look for max value of each month, because the data source is already altered to show only 1 price per month per share.

    Thank you

    • saud968's avatar
      saud968
      Icon for Memorable Member rankMemorable Member

      If the column is true or false, you should create a calculated column true = 1 and false = 2 and then use the measure and check

      • terdudov2's avatar
        terdudov2
        Frequent Visitor

        the column is not true/false, it is normal text so I do not understand that error