I'm trying to retrieve data from a collection B based on the values of one or more column of another collection A.
But the LookUp function doesn't work as it is expected to in this case.
ClearCollect(
A;
{
DateVal: DateValue("01/03/2024");
NNI: "A";
Presence: 0
};
{
DateVal: DateValue("04/03/2024");
NNI: "B";
Presence: 1
};
{
DateVal: DateValue("05/03/2024");
NNI: "C";
Presence: 0
};
{
DateVal: DateValue("06/03/2024");
NNI: "D";
Presence: 0
};
{
DateVal: DateValue("07/03/2024");
NNI: "E";
Presence: 0
}
)
;;
ClearCollect(
B;
{
DateVal: DateValue("01/03/2024");
DayNumber: 1
};
{
DateVal: DateValue("04/03/2024");
DayNumber: 4
};
{
DateVal: DateValue("05/03/2024");
DayNumber: 5
};
{
DateVal: DateValue("06/03/2024");
DayNumber: 6
};
{
DateVal: DateValue("07/03/2024");
DayNumber: 7
};
{
DateVal: DateValue("08/03/2024");
DayNumber: 8
}
);;
ClearCollect(
C;
ForAll(
A;
{
ADateVal: ThisRecord.DateVal;
BDayNumber: LookUp(B; DateVal = ThisRecord.DateVal; DayNumber)
}
)
)
Here's what I get for the C collection :
I expect to retrieve for each line the right BDayNumber corresponding to the DateVal which is exactly the same for both Collections.
In the LookUp expression, there are two places (scopes) where the value from 'DateVal' can come (table A and table B). Unless specified, the value will come to the nearest scope, which in this case is the table B. Same for ThisRecord: both ForAll and LookUp introduce this new scope, and it will use the closest one. So in the expression
ForAll(
A;
{
ADateVal: ThisRecord.DateVal;
BDayNumber: LookUp(B; DateVal = ThisRecord.DateVal; DayNumber)
}
)
The comparison in the LookUp expression is comparing the DateVal property for the B table with itself - so it will always return the first row.
You can make it look at the correct scope by using the As keyword to "give names" to the tables that are being used for the expression, as shown below:
ClearCollect(
A;
{
DateVal: DateValue("01/03/2024");
NNI: "A";
Presence: 0
};
{
DateVal: DateValue("04/03/2024");
NNI: "B";
Presence: 1
};
{
DateVal: DateValue("05/03/2024");
NNI: "C";
Presence: 0
};
{
DateVal: DateValue("06/03/2024");
NNI: "D";
Presence: 0
};
{
DateVal: DateValue("07/03/2024");
NNI: "E";
Presence: 0
}
)
;;
ClearCollect(
B;
{
DateVal: DateValue("01/03/2024");
DayNumber: 1
};
{
DateVal: DateValue("04/03/2024");
DayNumber: 4
};
{
DateVal: DateValue("05/03/2024");
DayNumber: 5
};
{
DateVal: DateValue("06/03/2024");
DayNumber: 6
};
{
DateVal: DateValue("07/03/2024");
DayNumber: 7
};
{
DateVal: DateValue("08/03/2024");
DayNumber: 8
}
);;
ClearCollect(
C;
ForAll(
A As TableA;
{
ADateVal: TableA.DateVal;
BDayNumber: LookUp(B As TableB; TableB.DateVal = TableA.DateVal; TableB.DayNumber)
}
)
)