Skip to main content
Announcements
NEW: Seamless Public Data Sharing with Qlik's New Anonymous Access Capability: TELL ME MORE!
cancel
Showing results for 
Search instead for 
Did you mean: 
melissapluke
Partner - Creator
Partner - Creator

Weighted Mode

Hi all,

I'm trying to get a Mode for Price based on the total occurrences per Procedure. Item is not taken into consideration, but does come in the base data so I've included it here. I can do this in a script or in the UI. I'd rather take care of it in the script however.

 

table1:
LOAD * INLINE [

Item, Procedure, Occurrence, Price

A, 7214F, 500, $333

B, 7214F, 1000, $250

C, 7214F, 250, $333

A, 7213F, 500, $600

B, 7213F, 100, $750

C, 7213F, 250, $203

A, 7212F, 500, $433

B, 7212F, 1000, $250

C, 7212F, 2150, $433
];

 

The results I'd want are:

Procedure, ModePrice

7214F, $250

7213F, $600

7212F, $433

Any ideas?

 

Thanks!

1 Solution

Accepted Solutions
sunny_talwar

That make sense now.. try this

table1:
LOAD * INLINE [
    Item, Procedure, Occurrence, Price
    A, 7214F, 500, $333
    B, 7214F, 1000, $250
    C, 7214F, 250, $333
    A, 7213F, 500, $600
    B, 7213F, 100, $750
    C, 7213F, 250, $203
    A, 7212F, 500, $433
    B, 7212F, 1000, $250
    C, 7212F, 2150, $433
];

Left Join (table1)
LOAD Procedure,
	 FirstSortedValue(Price, -Occurrence) as ModePrice
Resident table1
Group By Procedure;

View solution in original post

4 Replies
sunny_talwar

May be this

table1:
LOAD * INLINE [
    Item, Procedure, Occurrence, Price
    A, 7214F, 500, $333
    B, 7214F, 1000, $250
    C, 7214F, 250, $333
    A, 7213F, 500, $600
    B, 7213F, 100, $750
    C, 7213F, 250, $203
    A, 7212F, 500, $433
    B, 7212F, 1000, $250
    C, 7212F, 2150, $433
];

Left Join (table1)
LOAD Procedure,
	 Mode(Price) as ModePrice
Resident table1
Group By Procedure;

 

But that gives me this

image.png

For 7214F, I see the Price $333 repeating twice. Also, I don't really see anything repeating for 7213F, so not sure how would you pick 600? Are you looking for Median instead?

melissapluke
Partner - Creator
Partner - Creator
Author

Hi,

This is just a sample of the data. The issue i'm trying to solve is the $333 repeating itself. There will be lots of rows like this.  I'd like to pick the $250 instead because it has the highest number of occurrences per price, regardless of the "item" field.  Sorry if that wasn't clear before.  For procedure 7213F, I'd like to pick 600 because it has the highest number occurrences per price.

sunny_talwar

That make sense now.. try this

table1:
LOAD * INLINE [
    Item, Procedure, Occurrence, Price
    A, 7214F, 500, $333
    B, 7214F, 1000, $250
    C, 7214F, 250, $333
    A, 7213F, 500, $600
    B, 7213F, 100, $750
    C, 7213F, 250, $203
    A, 7212F, 500, $433
    B, 7212F, 1000, $250
    C, 7212F, 2150, $433
];

Left Join (table1)
LOAD Procedure,
	 FirstSortedValue(Price, -Occurrence) as ModePrice
Resident table1
Group By Procedure;
melissapluke
Partner - Creator
Partner - Creator
Author

Thanks, Sunny! That is what I was looking for!