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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
baadshah
Frequent Visitor

Identify records where the first created date meets a condition from a second variable

i have CRM data with where i need to create a new column which will index a '1' for all ID's where the first invoice issued 'CreatedDate' was paid ('State' = 2). So with the below example only row 2 is deemed as first invoice paid. 

Data context - Each unique 'ID' will have a unique 'ContractID' but many 'InvoiceID'. 

 

I've tried to nest CALCULATE(MIN(Table[CreatedDate) with RETURN but couldnt get it to work. 

 

Row

 

IDContractIDInvoiceIDCreatedDateModifiedDateState
1b6910000-00000-ed11-00000-0001000CONTRACT-0100BAGG3132220/04/202320/04/20230
2  BAG31268321/02/202323/05/20232
3  BAG31474309/05/202309/05/20230
4  BAG31561309/03/202309/03/20230

 

1 ACCEPTED SOLUTION
baadshah
Frequent Visitor

Hi @tamerj1 the above solution didnt work however i eventually got it to work in two steps.

1st - Identify the earliest date for each ID

Min_Date = CALCULATE(MIN(table[CreatedDate]),ALLEXCEPT(table,table[ID]))
 
2nd - Create another column to index with 1 for each row that meets Min_Date and state = 2
Commission_Pay = IF(table[State]=2 && table[CreatedDate] = table[Min_Date],1,0)

View solution in original post

6 REPLIES 6
baadshah
Frequent Visitor

Hi @tamerj1 the above solution didnt work however i eventually got it to work in two steps.

1st - Identify the earliest date for each ID

Min_Date = CALCULATE(MIN(table[CreatedDate]),ALLEXCEPT(table,table[ID]))
 
2nd - Create another column to index with 1 for each row that meets Min_Date and state = 2
Commission_Pay = IF(table[State]=2 && table[CreatedDate] = table[Min_Date],1,0)
tamerj1
Super User
Super User

Hi @baadshah 
For a calculated column please try

State =
INT (
    CALCULATE (
        MIN ( 'Table'[CreatedDate] ),
        ALLEXCEPT (
            'Table',
            'Table'[ID]
        )
    ) = 'Table'[CreatedDate]
) * 2

Hi @tamerj1 , this works for majority of my records but i have a handfull of 'State' = 0 appearing as "Paid" when 0 means Invoice wasnt paid. Is there anyway to tweak this measure to include the 'State'  column has to be 2 and cannot include records with 0

@baadshah 

I don't fully understand. Do you mean you have a column that flags paid invoices? Are we talking about a calculated column or a measure?

@tamerj1 

'State'
column = 2 indictates Paid and 0 indicates Unpaid. So effectively i'm looking for records by 'ID' that have the earlist/first 'CreatedDate'  and the 'State' column must be 2. So a nested calculation that will index 1 for each 'ID' records where the first 'CreatedDate' has a 'State' code = 2.

In the dummy data above row #2 meet that criteria because the min date is 21/02/2023 AND the state code = 2. Even if row #1 #3 and #4 had a state code = 2 it would still index 1 on the newly created column as row #2 as that has the min or earliest date. Hope that makes sense 🙂  

@baadshah 

Flag =
INT (
CALCULATE (
MIN ( 'Table'[CreatedDate] ),
ALLEXCEPT ( 'Table', 'Table'[ID] ),
'Table'[State] = 2
) = 'Table'[CreatedDate]
) * 2

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

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

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.