Forum Discussion

DjZijlstra's avatar
DjZijlstra
Frequent Visitor
4 years ago

count rows with 2 tables

Hi there,

 

I have 2 tables:

TblUnits          
UnitIDBusinessUnitIDBuildingIDUnitStatusIDUnitCodeUnitShortNameUnitTypeIDExploitationDateFromExploitationDateToOwnerContactIDObjectID 
176122865460720317-0040.04115522-12-2021 00:0028-3-2022 00:001479362543798Not in exploitation
176112865459820317-0030.03115522-12-2021 00:00 1479352543797Has no contract
176712865385720278-54BLOEMHOF 549834-2-2022 00:00 278442581121Has an contract
176502865512720344-43GROEN VAN PRINSTERERSTRAAT 4398320-1-2022 00:00 858592565732Has an contract

 

and 

 

TblContract         
ContractIDBusinessUnitIDContractNumberCustomerIDContactIDMainUnitIDDateContractSignedDateContractFromDateContractToObjectIDActief
176232862526319574148982176718-3-2022 00:008-3-2022 00:00 25988361
1757928625237195481532721765022-2-2022 00:0022-2-2022 00:00 25860271
1732628625118192821508111761122-12-2021 22:231-1-2022 00:0025-3-2022 00:0025438900

 

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? 

Thanks

4 Replies

  • PC2790's avatar
    PC2790
    Community Champion

    Hey DjZijlstra ,

     

    Try creating measures as below:

    Units with no contract = 
    VAR _unit = SELECTCOLUMNS(TblUnits,"UnitID", TblUnits[UnitID])
    VAR _inactivecontract = SELECTCOLUMNS(Filter(TblContract,TblContract[Actief] = 0),"UnitID",TblContract[MainUnitID])
    RETURN
    COUNTROWS(INTERSECT(_unit, _inactivecontract))
    Units with contract = 
    VAR _unit = SELECTCOLUMNS(TblUnits,"UnitID",TblUnits[UnitID])
    VAR _activecontract = SELECTCOLUMNS(Filter(TblContract,TblContract[Actief] = 1),"UnitID",TblContract[MainUnitID])
    RETURN
    COUNTROWS(INTERSECT(_unit, _activecontract))

     Results would be like this:

     

     

    • DjZijlstra's avatar
      DjZijlstra
      Frequent Visitor

      Thanks PC2790 

      the measure gives me a bit more info than before, but  it is not yet the number that is should be. In this case and example i used 1 contract on a unit, but there can be more contracts on 1 unit in the past that can be expired. So that must not be count. 

      is it possible to use the date i selected (on a calender) and the explationdate from and to from the unit and contractdates? 

      • PC2790's avatar
        PC2790
        Community Champion

        In that case, you can add a condition in the calculation of _unit to take into consideration the dates similar to what you have done above.

        See if that works for you