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

Announcements
Qlik GA: Multivariate Time Series in Qlik Predict: Get Details
cancel
Showing results for 
Search instead for 
Did you mean: 
xtrimf
Creator
Creator

sum from different fields

Hi,

Is it possible to write a dynamic expression in a pivot / straight table without "if" clause?

for example, an expression that sums a field according to row number :

row1, sum(Field1)

row2, sum(Field2) etc...\

something along "sum(Field&rowno())" that will work ? 🙂

1 Solution

Accepted Solutions
johnw
Champion III
Champion III

If for each category you "need a different calculation (not just sum(Field1))", then I am again failing to see what is wrong with either an if() or a pick(). You MUST specify every calculation SOMEWHERE. So there's no way around writing 100 calculations. The overhead of a pick() is negligible if you can use rowno().

pick(rowno()
,expression 1
,expression 2
,expression 3
...
,expression 100)

However, depending on how everything is laid out, you might be able to handle it in the script. I'm guessing you might be able to have a table with ID/expression/result fields:

ID expression result
12345 total sales 5000
12345 avarage monthly sales 300
12345 total cost 4000

And then your chart would have "expression" as a dimension. You'd probably set up the table with a crosstable load:

CROSSTABLE (expression,result)
LOAD
ID
,expression 1 as "total sales"
,expression 2 as "average monthly sales"
,expression 3 as "total cost"
...
,expression 100 as "whatever"
FROM YourTable
;

Now again, you're still having to maintain all 100 expressions, as there's no way around that. But you might get a performance benefit from doing it during the load rather than at execution time. Execution time might be more flexible, however, since it can for instance refer to fields from multiple tables.

I do think a weakness of QlikView is the ability to refer to different meanings of fields, in this case the inability (?) to assemble the field name with string commands and then just use it like a field.

View solution in original post

14 Replies
prieper
Master II
Master II

What would be the purpose / intension to do so? The usual way would be to have Field1, Field2 etc as Dimension and then Sum(xxx) as expression.

Peter

xtrimf
Creator
Creator
Author

It just gives me the freedom to use dynamic field names in a calculation,

it is very useful when for each dimension row you'd like to use a different calculation.. especially for P&L reports.

Not applicable

sum your values in your load script and display it in a straight table.

prieper
Master II
Master II

But then I think it might make even more sense to assign certain fields to a variable and to build then your pivot or other charts on basis this variable?

Peter

xtrimf
Creator
Creator
Author

it would still force me to use "IF" clause

xtrimf
Creator
Creator
Author

"Sum your values in your load script and display it in a straight table."

where's the fun in that? Wink

johnw
Champion III
Champion III


xtrim wrote:it would still force me to use "IF" clause


I guess I'm confused. What's wrong with using an IF clause? Are you worried about performance? In this case, the IF is outside of the summation, so it should be very efficient:

if(rowno()=1,sum(Field1)
,if(rowno()=2,sum(Field2)
,if(rowno()=3,sum(Field3))))

Not that it will perform any differently than an IF, but I'd probably use pick() instead for brevity:

pick(rowno(),sum(Field1),sum(Field2),sum(Field3))

Performance should only be a problem if you embed the pick() in the sum, so I don't recommend this:

sum(pick(rowno(),Field1,Field2,Field3))

But even there, it's possible that QlikView is smart enough to realize that rowno() doesn't have to be calculated every time, and will perform just fine.

Not applicable

The fun is in getting the job done fast and easy. Geeked

xtrimf
Creator
Creator
Author

Hi John,

I have about a hundred rows in each report...that is why i'm trying to avoid the if.