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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
DjZijlstra
Frequent Visitor

Count rows with different tables

Hi there,

 

I have 2 tables:

DjZijlstra_0-1649758988597.png

 

and 

DjZijlstra_1-1649759028894.png

 

 

there is an non-active relationship between MainUnitID and UnitID.

I use for the count of active contracts: CALCULATE( COUNTROWS(tblContract),
IF(AND(tblContract[DateContractFrom]<=Max(dates[date]),OR(tblContract[DateContractTo] >= Max(dates[date]), tblContract[DateContractTo]= 0)), 1,0))

 

I use for the count of active Units: CALCULATE(COUNTROWS(tblUnit), IF(AND(tblUnit[ExploitationDateFrom]<=Max(dates[date]),OR(tblUnit[ExploitationDateTo] >= Max(dates[date]), tblUnit[ExploitationDateTo]= 0)), 1,0))

 

I would like to count the free units (units that has no contract) 

In this example is the outcome 1 (nr. 17611) on the max(dates[date])  Max(dates[date]) is here today

 

I would like to count the units with contract 

In this example is the outcome 2 (nr. 17671 and 17650) on the max(dates[date])  Max(dates[date]) is here today

 

Can anyone help me? 

 

1 ACCEPTED SOLUTION

Hi @DjZijlstra ,

 

This is because I did not de-duplicate the TbIContract[MainUnitID] column, please try the modified measure:

 

count the units with contract = 
VAR tab_contract =
    FILTER (
        TbIContract,
        AND (
            TbIContract[DateContractFrom] <= MAX ( 'dates'[date] ),
            OR (
                TbIContract[DateContractTo] >= MAX ( 'dates'[date] ),
                TbIContract[DateContractTo] = 0
            )
        )
    )
VAR tab_units =
    FILTER (
        TbIUnits,
        AND (
            TbIUnits[ExploitationDateForm] <= MAX ( dates[date] ),
            OR (
                TbIUnits[ExploitationDateTo] >= MAX ( dates[date] ),
                TbIUnits[ExploitationDateTo] = 0
            )
        )
    )
RETURN
    CALCULATE (
        DISTINCTCOUNT(TbIContract[MainUnitID]),
        tab_contract,
        tab_units,
        USERELATIONSHIP ( TbIUnits[UnitID], TbIContract[MainUnitID] )
    )

vkkfmsft_0-1650015383373.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
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

4 REPLIES 4
v-kkf-msft
Community Support
Community Support

Hi @DjZijlstra ,

 

Please try the following measures:

 

count of active contracts = 
CALCULATE (
    COUNTROWS ( TbIContract ),
    FILTER (
        TbIContract,
        AND (
            TbIContract[DateContractFrom] <= MAX ( 'dates'[date] ),
            OR (
                TbIContract[DateContractTo] >= MAX ( 'dates'[date] ),
                TbIContract[DateContractTo] = 0
            )
        )
    )
)
count of active Units = 
CALCULATE (
    COUNTROWS ( TbIUnits ),
    FILTER (
        TbIUnits,
        AND (
            TbIUnits[ExploitationDateForm] <= MAX ( dates[date] ),
            OR (
                TbIUnits[ExploitationDateTo] >= MAX ( dates[date] ),
                TbIUnits[ExploitationDateTo] = 0
            )
        )
    )
)
count the units with contract = 
VAR tab_contract =
    FILTER (
        TbIContract,
        AND (
            TbIContract[DateContractFrom] <= MAX ( 'dates'[date] ),
            OR (
                TbIContract[DateContractTo] >= MAX ( 'dates'[date] ),
                TbIContract[DateContractTo] = 0
            )
        )
    )
VAR tab_units =
    FILTER (
        TbIUnits,
        AND (
            TbIUnits[ExploitationDateForm] <= MAX ( dates[date] ),
            OR (
                TbIUnits[ExploitationDateTo] >= MAX ( dates[date] ),
                TbIUnits[ExploitationDateTo] = 0
            )
        )
    )
RETURN
    CALCULATE (
        COUNTROWS ( TbIContract ),
        tab_contract,
        tab_units,
        USERELATIONSHIP ( TbIUnits[UnitID], TbIContract[MainUnitID] )
    )
free units = [count of active Units] - [count the units with contract]

vkkfmsft_0-1650000998628.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks @v-kkf-msft 

That kind of measure i had to try earlier, but that was not the number i was looking for. 

I have a file added (table copied below), where 1 unit has 2 contracts, and a unit has 2 contracts (1 active and 1 not active)

So if i count the contracts with these measures there a 3 contracts and 3 units, so 0 free. But thats not correct. There is 1 free. 

What measure can i use to get this number?

ContractBusinessUnitIDContractNoCustomerIDContactIDMainUnitIDDateContractFromDateContractToObjectIDActief

176232862526319574148982176718-3-2022null25988361
1757928625237195481532721765022-2-2022null25860271
1732628625118192821508111761122-12-202125-3-202225438900
176242862526419575148983176718-3-2022null25988361
1762528625265195751489841765022-2-202121-2-202225860270

 

Hi @DjZijlstra ,

 

This is because I did not de-duplicate the TbIContract[MainUnitID] column, please try the modified measure:

 

count the units with contract = 
VAR tab_contract =
    FILTER (
        TbIContract,
        AND (
            TbIContract[DateContractFrom] <= MAX ( 'dates'[date] ),
            OR (
                TbIContract[DateContractTo] >= MAX ( 'dates'[date] ),
                TbIContract[DateContractTo] = 0
            )
        )
    )
VAR tab_units =
    FILTER (
        TbIUnits,
        AND (
            TbIUnits[ExploitationDateForm] <= MAX ( dates[date] ),
            OR (
                TbIUnits[ExploitationDateTo] >= MAX ( dates[date] ),
                TbIUnits[ExploitationDateTo] = 0
            )
        )
    )
RETURN
    CALCULATE (
        DISTINCTCOUNT(TbIContract[MainUnitID]),
        tab_contract,
        tab_units,
        USERELATIONSHIP ( TbIUnits[UnitID], TbIContract[MainUnitID] )
    )

vkkfmsft_0-1650015383373.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks @v-kkf-msft .

Thats what i'm looking for. 

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

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 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.