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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
lukeh1995
Frequent Visitor

New Column in Table based on TWO other tables - IF Statement

Hi all,

I have a model with multiple tables containing salary information from staff, with the $ amounts coming from two seperate tables. I am wanting to create a NEW COLUMN in my fact table that pulls the data from one table IF the unique position number is in that table, otherwise from another table that contains the unique position number. Is this possible?

1 ACCEPTED SOLUTION

Hi @lukeh1995 ,

 

I suggest you to try code as below to create a calculated column.

Column = 
VAR _Position1 =
    VALUES ( Dimtable1[Position] )
VAR _Position2 =
    VALUES ( Dimtable2[Position] )
VAR _CurPosition = 'Fact Table'[Position]
RETURN
    SWITCH (
        TRUE (),
        _CurPosition IN _Position1,
            CALCULATE (
                SUM ( Dimtable1[Amounts] ),
                FILTER ( Dimtable1, Dimtable1[Position] = EARLIER ( 'Fact Table'[Position] ) )
            ),
        _CurPosition IN _Position2,
            CALCULATE (
                SUM ( Dimtable2[Amounts] ),
                FILTER ( Dimtable2, Dimtable2[Position] = EARLIER ( 'Fact Table'[Position] ) )
            )
    )

Result is as below.

vrzhoumsft_0-1694419821345.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
danextian
Super User
Super User

Hi @lukeh1995 ,

Try the following approaches:

If there is an existing relationship between the two tables

= RELATED ( 'Table1',[Column] )

If there isn't

= LOOKUPVALUE ( 'Table1'[Position], 'Table1'[Desired Column],  'Fact'[Position] )
=
CALCULATE (
    MAX ( 'Table1'[Desired Column] ),
    FILTER ( 'Table1', 'Table1'[Position] = EARLIER ( 'Fact'[Position] ) )
)

These columns are to be created in fact.

 

If none of these works, please provide context to your post by providing a sample data.

https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/td-p/144...

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Hi!

Thank you so much for your response.

I understand 'related' would work for two tables, but I have three: I am wanting to fill the values in the fact table from one dimension table if the primary key exists in it, else another table.

Hi @lukeh1995 ,

 

I suggest you to try code as below to create a calculated column.

Column = 
VAR _Position1 =
    VALUES ( Dimtable1[Position] )
VAR _Position2 =
    VALUES ( Dimtable2[Position] )
VAR _CurPosition = 'Fact Table'[Position]
RETURN
    SWITCH (
        TRUE (),
        _CurPosition IN _Position1,
            CALCULATE (
                SUM ( Dimtable1[Amounts] ),
                FILTER ( Dimtable1, Dimtable1[Position] = EARLIER ( 'Fact Table'[Position] ) )
            ),
        _CurPosition IN _Position2,
            CALCULATE (
                SUM ( Dimtable2[Amounts] ),
                FILTER ( Dimtable2, Dimtable2[Position] = EARLIER ( 'Fact Table'[Position] ) )
            )
    )

Result is as below.

vrzhoumsft_0-1694419821345.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.