[或运算集分析-Qlik Sense

问题描述 投票:0回答:2

这是集合分析中的一个典型和/或问题,我被困了很长时间。

enter image description here

我想对这些ID的数量求和,其中:

  1. type_of_entry既是“收入和支出”又是“收入和劳动”

  2. 收入类型为'CAF'

预期的ID以粗体显示

例如... id 1对于收入和费用均存在。同样,收入和人工均存在ID 2和ID 3。

结果->数量= 55(5 + 40 + 10)

我已经尝试了以下集合分析,但是没有用:

enter image description here

我将不胜感激。

问候

Sagnik

qliksense set-analysis
2个回答
0
投票

您接受答案是Python解决方案吗?

python解决方案

import pandas as pd
from collections import defaultdict

df = pd.DataFrame([
    ['Expense', 1, 10, '-'],
    ['Labor', 2, 20, '-'],
    ['Labor', 3, 50, '-'],
    ['Revenue', 1, 5, 'CAF'],
    ['Revenue', 2, 30, 'NORM'],
    ['Revenue', 2, 40, 'CAF'],
    ['Revenue', 3, 10, 'CAF'],
    ['Revenue', 4, 20, 'NORM'],
    ['Revenue', 5, 30, 'CAF']
], columns=['type_of_entry', 'id', 'amount', 'revenue_type'])

series_caf = df[df['revenue_type'].eq('CAF')]
filter_id_list = series_caf['id'].to_list()  # 1, 2, 3, 5
result_amount = 0
dict_ok = defaultdict(list)
for cur_id in filter_id_list:
    is_revenue = len(df[(df.id == cur_id) & (df.type_of_entry == 'Revenue')]) > 0
    is_expense = len(df[(df.id == cur_id) & (df.type_of_entry == 'Expense')]) > 0
    is_labor = len(df[(df.id == cur_id) & (df.type_of_entry == 'Labor')]) > 0
    is_ok = (is_revenue and is_expense) or (is_revenue and is_labor)
    if is_ok:
        cur_amount = series_caf[series_caf.id == cur_id].amount.values[0]
        result_amount += cur_amount
        dict_ok['id'].append(cur_id)
        dict_ok['amount'].append(cur_amount)
        dict_ok['ok_reason (REL)'].append(is_revenue*100+is_expense*10+is_labor)

df_result_info = pd.DataFrame.from_dict(dict_ok)
print(df_result_info)
print(result_amount)

输出

   id  amount  ok_reason (REL)
0   1       5              110
1   2      40              101
2   3      10              101
55

0
投票

脚本-

enter image description here

p()函数根据您的过滤器提取可能的值,在本例中为Expense and Labor,*运算符执行and运算。简而言之,您可以拥有所有所需的id,然后应用Revenue_type过滤器。

类似地,有一个e()函数提取排除的值。

这个答案不是我的,Sunny Talwar先生帮助我找到了这个问题的解决方案。有效。

© www.soinside.com 2019 - 2024. All rights reserved.