Forum Discussion

RafDelgado's avatar
RafDelgado
Helper II
4 years ago
Solved

Join or add column based on multiple date range and categories

 

Hey All, 

 

I'm working with two tables that contain 44k rows of data from Dataverse and an API web call. One table has the customer information and the type of service they had during a period. The second is a table which shows all the data time entries by everyone. 

 

CRM Data Verse

CustomerStartDateEndDateServiceTypeContractName
Customer A12/04/201812/04/2019Service Type 1Contract 1
Customer B12/07/201812/07/2019Service Type 1Contract 2
Customer A13/06/201813/06/2022Service Type 2Contract 3

 

API

AgentServiceTypeCustomerCreation DateTimeSpent
Agent AService Type 1Customer A22/04/2018150
Agent BService Type 2Customer A14/06/2020160
Agent AService Type 1 Customer B13/07/20195

 

I created a list first, which on query editor works as expected, but it's ridiculously slow to load on refresh as it's almost manually checking on each 44k row.

 

List.First(
Table.SelectRows(
Contract,
(r) =>
r[StartDate] < [CreatedDate]
and
r[EndDate] > [CreatedDate]
and
r[Customer] = [Customer]
and
r[ServiceType] = [ServiceType]
)[ContractName]
)

 

 

 

Is there a way to add a column to the second table on the data model based on multiple conditions between X date that matches multiple columns and returns a string value that's a quicker way to join? Like the below?

 

API

AgentServiceTypeCustomerCreation DateTimeSpentContractName
Agent AService Type 1Customer A22/04/2018150Contract 1
Agent BService Type 2Customer A14/06/2020160Contract 2
Agent AService Type 2Customer B13/07/20195Contract 3
Agent BService Type 1Customer A23/07/2018100Contract 1

 

  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi RafDelgado ,

    Please have a try.

    Create a measure.

    Measure =
    CALCULATE (
        MAX ( TableA[ContractName] ),
        FILTER (
            ALL ( TableA ),
            TableA[ServiceType] = SELECTEDVALUE ( API[ServiceType] )
                && TableA[Customer] = SELECTEDVALUE ( API[Customer] )
        )
    )
    

    Or a column.

    column =
    CALCULATE (
        MAX ( TableA[ContractName] ),
        FILTER (
            ( TableA ),
            TableA[ServiceType] = EARLIER ( API[ServiceType] )
                && TableA[Customer] = EARLIER ( API[Customer] )
        )
    )
    

     

    Best Regards

    Community Support Team _ Polly

     

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

1 Reply

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi RafDelgado ,

    Please have a try.

    Create a measure.

    Measure =
    CALCULATE (
        MAX ( TableA[ContractName] ),
        FILTER (
            ALL ( TableA ),
            TableA[ServiceType] = SELECTEDVALUE ( API[ServiceType] )
                && TableA[Customer] = SELECTEDVALUE ( API[Customer] )
        )
    )
    

    Or a column.

    column =
    CALCULATE (
        MAX ( TableA[ContractName] ),
        FILTER (
            ( TableA ),
            TableA[ServiceType] = EARLIER ( API[ServiceType] )
                && TableA[Customer] = EARLIER ( API[Customer] )
        )
    )
    

     

    Best Regards

    Community Support Team _ Polly

     

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