Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Toronto Sept 9th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Running Median calculation

Hi All,

Need help in calculating Running median in a Line chart. Requirement as below. For a given date, the expression should consider all the Age values till that date and calculate the median. For all the missing dates from min date to max date, previous date Median value should be repeated. Please let me know if you need more information.

Data :-

DateBUG_IDAge
29-Jan-15266634
4-Feb-15231328
4-Feb-152097531
24-Feb-152015354
2-Mar-15230916
2-Mar-15155828
2-Mar-152103523

Calculation :-

On 29-Jan-15 -> Median is 4

On 4-Feb-15 -> Median (4,8,31) = 8

On 24-Feb-15 -> Median(4,8,31,54) = 19.5

On 2-Mar-15 -> Median(4,8,31,54,6,8,23) = 8

Result :-

DateMedian Age
29-Jan-154
30-Jan-154
31-Jan-154
1-Feb-154
2-Feb-154
3-Feb-154
4-Feb-158
5-Feb-158
6-Feb-158
7-Feb-158
8-Feb-158
9-Feb-158
10-Feb-158
11-Feb-158
12-Feb-158
13-Feb-158
14-Feb-158
15-Feb-158
16-Feb-158
17-Feb-158
18-Feb-158
19-Feb-158
20-Feb-158
21-Feb-158
22-Feb-158
23-Feb-158
24-Feb-1519.5
25-Feb-1519.5
26-Feb-1519.5
27-Feb-1519.5
28-Feb-1519.5
1-Mar-1519.5
2-Mar-158

Regards,

KKR

15 Replies
Kushal_Chawda

kumar_ilb

It should be possible to calculate the Median at script level. On presentation layer it will be more complex.

Can you share the sample data with other fields also?

Not applicable
Author

Attached sample data and expected result set. Let me know if you need more information.

Regards,

KKR

sunny_talwar

Here is a front end solution. See if this is useful:

Capture.PNG

Expression: =If(Count({<Date = >}TOTAL <Date> Date) = RowNo(), RangeFractile(0.50, Above(TOTAL Only({<Date = >} Age), 0, RowNo(TOTAL))))

Dimensions: Date and BUG_ID

Note: Used BUG_ID to create a unique list of Age for each row and then hid the dimension from the presentation tab. If in your application you will have another dimension which makes Age unique for each row, you will probably need to add that to your chart.

Attaching the qvw for you to review.

HTH

Best,

Sunny

sunny_talwar

Without selection it is working the way you want, still need to figure it out after selection.

Script:

Table:

LOAD PROJECT_ID,

     APPLICATION,

     VERSION,

     BUD_ID,

     FIND_DATE,

     CLOSE_DATE,

     CLOSE_DATE - FIND_DATE as Age

FROM

[FD SampleData.xlsx]

(ooxml, embedded labels, header is 1 lines, table is Sheet1);

Temp: 

Load min(FIND_DATE) as minDate, 

     max(CLOSE_DATE) as maxDate 

Resident Table; 

 

Let varMinDate = Num(Peek('minDate', 0, 'Temp')); 

Let varMaxDate = Num(Peek('maxDate', 0, 'Temp')); 

DROP Table Temp; 

 

MasterCalendar:

LOAD Date($(varMinDate) + IterNo() - 1) as CLOSE_DATE 

AutoGenerate 1

While $(varMinDate) + IterNo() -1 <= $(varMaxDate);

Straight Table

Dimension: CLOSED_DATE

                 BUD_ID

Expression:

=If(Count({<CLOSE_DATE = >}TOTAL <CLOSE_DATE> CLOSE_DATE) = RowNo(),

If(Len(RangeFractile(0.50, Above(TOTAL Only({<CLOSE_DATE = >} Age), 0, RowNo(TOTAL)))) = 0, 0.0000000001,

RangeFractile(0.50, Above(TOTAL Only({<CLOSE_DATE = >} Age), 0, RowNo(TOTAL)))))

Output:

Capture.PNG

sunny_talwar

Here you go. Seems to be working the way you want, but I leave it for you to do the final testing

Same Script as Above. Dimension remains the same. Only the expression changes:

=If(Count({<CLOSE_DATE = , PROJECT_ID = >}TOTAL <CLOSE_DATE> CLOSE_DATE) = RowNo(),

If(Len(RangeFractile(0.50, Above(TOTAL Only({<CLOSE_DATE = >} Age), 0, RowNo(TOTAL)))) = 0, 0.0000000001,

RangeFractile(0.50, Above(TOTAL Only({<CLOSE_DATE = >} Age), 0, RowNo(TOTAL)))))

Screenshot after Selection:

Capture.PNG

Not applicable
Author

I have created a new Date table with two columns Date_FD and DateKey. Date_FD contains dates from minimum CLOSE_DATE to maximum. For each Date_FD, DateKey will contain dates from minimum CLOSE_DATE to the Date_FD value. Below is the script.

In line chart, used Date_FD as dimension and Median(Age) as expression.

DATA:

LOAD PROJECT_ID,

     APPLICATION,

     VERSION,

     BUD_ID,

     FIND_DATE,

     CLOSE_DATE,

     CLOSE_DATE as DateKey,

     CLOSE_DATE - FIND_DATE as Age

FROM

[FD SampleData.xlsx]

(ooxml, embedded labels, header is 1 lines, table is Sheet1);

MAX_MIN_TEMP:

LOAD

    Min(CLOSE_DATE) as MIN_DATE,

    Max(CLOSE_DATE) as MAX_DATE

Resident DATA;

LET vMinDate = Num(Peek('MIN_DATE'));

LET vMaxDate = Num(Peek('MAX_DATE'));

FOR i=$(vMinDate) to $(vMaxDate)

FD_CALENDAR:

LOAD

    Date($(i)) as Date_FD,

    Date($(vMinDate)+RecNo()-1) as DateKey

AutoGenerate $(i)-$(vMinDate)+1;  

NEXT  

DROP Table MAX_MIN_TEMP;

Regards,

KKR