将列中的数据转换为行 - Qlikview Load

Converting data in columns to rows - Qlikview Load

我有一个 table 如下所示 excel sheet:

Person Food Drink Snack
A Sandwich Water Crisps
B Wrap Coke Crisps
C Pasta Coke Crisps
D Sandwich Juice Banana
E Burger Coke Crisps

我想在将数据加载到 qlikview 时将其转换为以下 table

Person Item
A Sandwich
A Water
A Crisps
B Wrap
B Coke
B Crisps
C Pasta
C Coke
C Crisps
D Sandwich
D Juice
D Banana
E Burger
E Coke
E Crisps

有办法实现吗?

可能最懒惰的方法是遍历所有字段并将它们连接起来。

下面带注释的脚本将产生以下结果:

RawData:
LOAD 
    Person, 
    Food, 
    Drink, 
    Snack
FROM
    [C:\Users\User\Documents\data.xlsx]
    (ooxml, embedded labels, table is Sheet1)
;

// loop through all the fields in RawData table
for i = 1 to NoOfFields('RawData')
    // get the name of the field in the current iteration
    let fieldName = FieldName($(i), 'RawData');
    
    // field "Person" should not be included    
    if('$(fieldName)' <> 'Person') then
        // resident load from RawData table the Person field
        // and the field name from the current iteration
        // name the new field "Item"
        // because the table in each iteration is having the same 
        // name and the same fields Qlik will concatenate the result
        // to an existing table
        Result:
        Load
            Person,
            [$(fieldName)] as Item 
        Resident
            RawData
        ;
    
    end if

next

// if RawData table is not needed anymore
Drop Table RawData;

另一种方法是使用交叉表。

Data:
load * inline [
Person, Food,   Drink,  Snack
A,  Sandwich,   Water,  Crisps
B,  Wrap,   Coke,   Crisps
C,  Pasta,  Coke,   Crisps
D,  Sandwich,   Juice,  Banana
E,  Burger, Coke,   Crisps];

NEW_Table:
crosstable(Name,Item,1)
load * resident Data;

drop table Data;
drop field Name;