Forum Discussion

Syndicate_Admin's avatar
Syndicate_Admin
Administrator
4 years ago
Solved

Get records between two dates

I have a table that has as a key the name of the person and has a date data this table is called patients.

NumberDateStatureWeight
Santiago12/05/2022
Laura1/03/2021
Marcela6/07/2021

In another table you have a historical record of the height and weight of patients, this table is called historicalPatients.

Number DateStatureWeight
Santiago1/01/202214055
Santiago1/03/202214256
Santiago1/05/202214557
Laura1/01/202115052
Laura1/04/202115353
Laura1/06/202115654
Marcela1/04/202113556
Marcela1/06/202113758
Marcela7/08/202113960

It is required to feed the data of the patient table comparing the record where the date of the patient table is greater than the historical one but not greater than the following record, that is, for the case of marcela, the date in the patient table is 6/07/2021 and in the historical table it is fulfilled that the date is greater than the record 1/04/2021 and the record 1/06/2021 should be taken the second since it is the closest and once this record is identified, bring the height and weight data in the patient table. The result is expected to be as follows:

NumberDateStatureWeight
Santiago12/05/202214557
Laura1/03/202115052
Marcela6/07/202113758

  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi Syndicate_Admin ,

     

    Please check these formulas.

    latest date = CALCULATE(MAX(HistoricalPatients[Date]),FILTER(HistoricalPatients,HistoricalPatients[Date]<=Patients[Date]&&HistoricalPatients[Number]=Patients[Number]))
    
    Stature = CALCULATE(MAX(HistoricalPatients[Stature]),FILTER(HistoricalPatients,HistoricalPatients[Number]=Patients[Number]&&HistoricalPatients[Date]=Patients[latest date]))
    
    Weight = CALCULATE(MAX(HistoricalPatients[Weight]),FILTER(HistoricalPatients,HistoricalPatients[Number]=Patients[Number]&&HistoricalPatients[Date]=Patients[latest date]))

     

    Best Regards,

    Jay

5 Replies

  • Hi,

    Write these calculated column formulas in the Patient table

    =LOOKUPVALUE(Historicalpatients[Stature],Historicalpatients[Date],CALCULATE(max(Historicalpatients[Date]),FILTER(Historicalpatients,Historicalpatients[Number]=EARLIER(Patients[Number])&&Historicalpatients[Date]<=EARLIER(Patients[Date]))),Historicalpatients[Number],Patients[Number])
    =LOOKUPVALUE(Historicalpatients[Weight],Historicalpatients[Date],CALCULATE(max(Historicalpatients[Date]),FILTER(Historicalpatients,Historicalpatients[Number]=EARLIER(Patients[Number])&&Historicalpatients[Date]<=EARLIER(Patients[Date]))),Historicalpatients[Number],Patients[Number])

    Hope this helps.

    • Syndicate_Admin's avatar
      Syndicate_Admin
      Administrator

      I tried but I get the error I provided a table of several values where a single value was expected, it is important to comment that in the historical table there is a row where the name is found but we do not have associated dates yet then empty values can be found, is it possible that this is not serving the lookupvalue formula?

      • Syndicate_Admin's avatar
        Syndicate_Admin
        Administrator

        You can now fix the error by adding an IF clause for the when the date value in historical is empty leave the cell blank but if you find a date data bring the height and weight, thank you very much.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Syndicate_Admin ,

     

    Please check these formulas.

    latest date = CALCULATE(MAX(HistoricalPatients[Date]),FILTER(HistoricalPatients,HistoricalPatients[Date]<=Patients[Date]&&HistoricalPatients[Number]=Patients[Number]))
    
    Stature = CALCULATE(MAX(HistoricalPatients[Stature]),FILTER(HistoricalPatients,HistoricalPatients[Number]=Patients[Number]&&HistoricalPatients[Date]=Patients[latest date]))
    
    Weight = CALCULATE(MAX(HistoricalPatients[Weight]),FILTER(HistoricalPatients,HistoricalPatients[Number]=Patients[Number]&&HistoricalPatients[Date]=Patients[latest date]))

     

    Best Regards,

    Jay