Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
agsearle
Creator
Creator

Calculated field

I am running a report on water meter readings, below is the result of one meter, showing the date the meter was read, together with the reading.

Firstly in the load statement how can I only select the two most recent readings?

Reading DateReading
19/03/20072499
26/06/20072574
20/09/20072655
12/12/20072722
13/03/20082797
16/05/20082874
16/05/20082874
13/06/20082899
18/07/20082929
18/07/20082929
19/09/20082985
12/12/20083053
13/03/20093128


Then with reference to those last two readings I need to identify any meters where the reading has not increased since the previous reading. I was thinking of having another column which would show the difference of the reading from the one above it.

Is this possible, and if so how?

1 Solution

Accepted Solutions
agsearle
Creator
Creator
Author

Excellent Aaron, liked the second part, worked well thanks. It now shows all the readings that have not changed.

Just one more thing though, but very similar. I also need to identify all meters where the difference between the readings is less than 5. This will show meters that are slowing, but not quite stopped.

Can a similar script work for these?

View solution in original post

9 Replies
Oleg_Troyansky
Partner Ambassador/MVP
Partner Ambassador/MVP

For the two most recent readings, - I can't think of an elegant solution that would allow doing it in one load... AS a work around, you could do it in two steps:

1. Load all readings sorted by Reader and Date descenting and assign numbers to each reading - something like this:


load
Reader,
Date,
Reading,
if(Peek(Reader) <> Reader, 0, previous(ReadingNumber)) as ReadingNumber,
if(Peek(Reader) <> Reader, Reading, Reading - previous(Reading)) as ReadingDifference
resident
Readings
Order By
Reader, Date DESC
;


Then, as a second step, reload from this table only selecting ReadingNumber <=2

Between peek() and previous(), this should work. I always forget which one works better in what situation...

good luck!

Oleg

agsearle
Creator
Creator
Author

Can't seem to get that to work.

More importantly though, is the second part of my problem. See below:

InstallationReading DateReading
4000010316/05/20082874
4000010316/05/20082874
4000010313/06/20082899
4000010318/07/20082929
4000010318/07/20082929
4000010319/09/20082985
4000010312/12/20083053
4000010313/03/20093128
4000012304/04/2008143
4000012309/10/2008165
4000013030/04/2008393
4000013030/10/2008488
4000015212/06/2008208
4000015208/12/2008209
4000015823/08/200840633
4000015806/02/200942143


This is a selection of the data in the report. So according to each installation, where the reading date is not the same, I need to know what the difference between each reading is, in a new column.

Complicated I know, but I'm sure someone knows how to do it? Big Smile

Not applicable

I want to make sure I understand this...

In a table that is grouped by Installation and Reading Date, you want to see the difference in a 4th column if more than one Reading value is present for an Installation/Reading Date?

agsearle
Creator
Creator
Author

Yes that's it exactly.

The idea being that it will identify any meters that are not recording any water between readings, so we can investigate them.

Not applicable

You will need to make a field that concatenates the Installation and ReadingDate in the script to use in the set expression first but then this should work (syntax may need to be adjusted as well).

=Max({$<InstallationReadingDateField = {'=NumericCount(DISTINCT Reading)>1'}>} Reading) - Min({$<InstallationReadingDateField = {'=NumericCount(DISTINCT Reading)>1'}>} Reading)

This would take the max value on any dates with more than one distinct reading and subtract the minimum value from it giving you a variance for the entire day. The nice part is that the formula would then also be scalable for any number of readings in a single day.

It sounds, though, like you may need something different so it only shows installations with the same reading since those would be the ones not working in which case this would be better in a table with Installation and Reading Date.

=If(NumericCount(DISTINCT Reading)=1 AND NumericCount(Reading)>1, 'Identifier Text')

agsearle
Creator
Creator
Author

Excellent Aaron, liked the second part, worked well thanks. It now shows all the readings that have not changed.

Just one more thing though, but very similar. I also need to identify all meters where the difference between the readings is less than 5. This will show meters that are slowing, but not quite stopped.

Can a similar script work for these?

Not applicable

The first expression should work, but you'll need to add a bit to it. It will need an if statement to exclude any variance over 5.

If(Max({$<InstallationReadingDateField = {'=NumericCount(DISTINCT Reading)>1'}>} Reading) - Min({$<InstallationReadingDateField = {'=NumericCount(DISTINCT Reading)>1'}>} Reading)<=5, Max({$<InstallationReadingDateField = {'=NumericCount(DISTINCT Reading)>1'}>} Reading) - Min({$<InstallationReadingDateField = {'=NumericCount(DISTINCT Reading)>1'}>} Reading))

agsearle
Creator
Creator
Author

All sounds good, and I've tried to incorporate this into my report, but I have a problem.

I'm not able to create the concatenated field 'InstallationReadingDateField', as the two fields are in different tables. I've tried to map them into one, but then can't concatenate them.

Here's what I've got so far, and I've underlined the part that doesn't work.

EABLG_MAP:
MAPPING LOAD Internal_ID_For_Meter_Reading_Document,
Installation
FROM (qvd)
WHERE EXISTS (Installation);

EABL:
LOAD Internal_ID_For_Meter_Reading_Document,
Meter_Reading_Date,
Equipment_Number,
Device,
Meter_Reading_Type,
Places_Before_Decimal_Point_In_Meter_Reading as Reading,
APPLYMAP ('EABLG_MAP', Internal_ID_For_Meter_Reading_Document) as Installation_EABL,
Installation_EABL & ' ' & Meter_Reading_Date as Installation_Reading_Date,
Register
FROM (qvd)
WHERE EXISTS (Equipment_Number)
AND Meter_Reading_Type = '01'
OR Meter_Reading_Type = '02'
OR Meter_Reading_Type = '03'
OR Meter_Reading_Type = '06'
OR Meter_Reading_Type = '09'
OR Meter_Reading_Type = '21'
OR Meter_Reading_Type = '22';

Any new idea's? [:'(]

Oleg_Troyansky
Partner Ambassador/MVP
Partner Ambassador/MVP

Andrew,

you are trying to use a new field Installation_EABL in the same load statement where you calculated it - you can't do that. The field is not available yet at that point. You need to either use a preceding load or repeat your APPLYMAP statement once again, like this:

APPLYMAP ('EABLG_MAP', Internal_ID_For_Meter_Reading_Document) as Installation_EABL,
APPLYMAP ('EABLG_MAP', Internal_ID_For_Meter_Reading_Document) & ' ' & Meter_Reading_Date as Installation_Reading_Date,

Also, as a suggestion - you need to add a default value for your applymap, just in case an the Date doesn't exist, otherwise you'll get the Internal_ID instead of the date.

good luck!

Oleg