一种在Qlik Sense 中计算两个年月所覆盖时间跨度的方法以及场景应用
背景:筆者在一個B2b電商平臺從事數據分析工作,公司選取Qlik Sense作為BI分析工具;當前的業務需求是分析每個月份的客戶下單情況,這需要得出每個客戶的注冊年月、下單年月以及下單月份數。由于Qlik Sense在國內的資料較少,筆者在工作之余總結、撰寫此文,旨在分享并盼能拋磚引玉,得到更多、更好的想法,不僅可以用于Qlik,也可在其他BI工具實踐。
一、計算方法
設: 開始年=Fy, 開始月=Fm, 開始年月=Fym
設: 結束年=Ly, 結束月=Lm, 結束年月=Lym
年月=Year * 100 + Month
默認: Ly >= Fy; Fm & Lm ∈ [1,12]
Time Span = {[(13-Fm) + Lm] + (Ly – Fy -1)*12}
Or: {[(Fy*100 + 13) – Fym] + (Lym – Ly*100) + (Ly-Fy-1)*12}
13-Fm: 開始年當年月份數。從當月開始至12月,共13-Fm個月,當月本身也計算在內(如9月-12月,共4個月;12月-12月,共1個月)
Lm: 結束年當年月份數。從1月至結束月,共Lm個月,結束月本身也計算在內(如1月-5月,共5個月,1月-8月,共8個月)
(Ly-Fy-1)*12: 從開始年至結束年的月份數
考慮年份差別,共有三種情況。
1.如開始年和結束年為同一年,Ly=Fy, 則時間跨度= 13-Fm+Lm-12, 即 Lm-Fm+1,結束月減去開始月+1;
2.如結束年為開始年的下一年,Ly = Fy+1, 則時間跨度 = 13-Fm+Lm, 即開始年的所有月份數量(13-Fm)加上結束年的所有月份數量Lm;
3.如結束年比開始年晚兩年及以上,則時間跨度 = 開始年當年月份數+結束年當年月份數 + (開始年與結束年相隔年份的所有月份數)
二、場景應用
計算客戶注冊年月與當前下單年月的月份數,并與客戶在當前下單年月的‘總下單月份數’進行比較,計算客戶對于平臺的忠誠度
三、Qlik腳本步驟
出于數據隱私性,本文使用編撰的數據進行演示說明,并盡可能的精簡了數據。
需要的基礎數據:訂單列表,客戶注冊信息表
客戶注冊信息表字段:customer_code; customer_name; register_date
訂單列表字段:order_date; order_number; customer_code; commodity; order_amount
以下所有的計算均在Qlik Sense的腳本編輯器完成。
(1) 加載訂單列表數據:
[orderlist]:Load * Inline [order_date, order_number, customer_code, commodity, order_amount2021/1/5, O2021001, T001, Milk, 10.52021/1/20, O2021002, T001, Cheese, 52021/3/2, O2021003, T001, Ham, 202021/3/7, O2021004, T002, Apple, 82021/4/10, O2021005, T001, Banana, 92021/5/18, O2021006, T001, Pie, 302021/6/1, O2021007, T001, Coke, 72021/6/1, O2021008, T002, Butter, 62021/6/1, O2021009, T003, Bone, 182021/7/1, O2021010, T003, Steak, 1002021/8/8, O2021011, T003, Bacon, 602021/8/12, O2021012, T001, Egg, 402021/8/20, O2021013, T002, Candy, 152021/9/6, O2021014, T003, Potato, 222021/10/1, O2021015, T003, Bean, 202021/10/15, O2021016, T001, Chocolate, 122021/11/11, O2021017, T003, Pork, 702021/11/17, O2021018, T002, Chicken, 302021/12/10, O2021019, T002, Bread, 142021/12/12, O2021020, T001, Fish, 562021/12/15, O2021021, T003, Ketchup, 242022/1/1, O2021022, T003, Jam, 35];(2) 加載客戶注冊信息表,并與訂單列表進行左關聯:
Left Join [orderlist]:Load * Inline [customer_code, customer_name, register_dateT001, Tom, 2021/1/1,T002, Jerry, 2021/3/1,T003, Spike, 2021/6/1];合并后的數據表如下圖:
?
(3) 根據合并后的訂單列表 [orderlist],得出每個客戶的注冊年,registeryear, 注冊年月registerym,用數字形式表現,即registeryear*100+registermonth; 同理,得到每個客戶的下單年orderyear和下單年月orderym
[orderwidetable]:Load *,Year(register_date) as [registeryear],Year(register_date)*100 + Month(register_date) as [registerym],Year(order_date) as [orderyear],Year(order_date)*100 + Month(order_date) as [orderym]Resident [orderlist];Drop Table [orderlist];生成的數據表如下圖:
?
(4) 基于第三步生產的寬表 [orderwidetable],以客戶維度聚合一張新表,求每個客戶的:首次下單年firstyear;首次下單年月firstym;總下單月數totalordermonth
[ordermonth]:Load[customer_code] as [ccode],[registerym] as [rym],[registeryear] as [ryear],min(orderym) as [firstym],min(orderyear) as [firstyear],count(distinct [orderym]) as [totalordermonth]Resident [orderwidetable]Group By [customer_code],[registerym],[registeryear];生成的數據表如下圖:
?
?(5) 同樣基于 [orderwidetable],以客戶和下單年月維度聚合一張新表,求每個客戶的每個不同的下單年月
[count]:Load[customer_code] as [ccode],[orderym] as [oym],[orderyear] as [oyear],count(distinct orderym) as [countym]Resident [orderwidetable]Group By [customer_code],[orderym],[orderyear]Order By [customer_code],[orderym];生成數據表如下圖:
?(6) 將第四步生成的表左關聯到第五步生成的表,這樣,每一行的客戶下單年月,都能顯示客戶的首單年份、首單年月、總下單月數
Left Join [count]:Load*Resident [ordermonth];Drop Table [ordermonth];生成數據表如下圖:
?
(7) 基于第六步生成的表格,應用時間跨度計算方法,求每個客戶的每個下單年月與首單年月的間隔月數 [lifemonth],并計算每個客戶在每個下單年月時的累計下單月份數 [ordermonth]
求累計下單月數參考了Qlik Community的一篇文章,主要使用了Peek函數
鏈接:Solved: Script: cumulative sum + group by - Qlik Community - 1810410
[count_temp]:Load*,(([ryear]*100+13-[rym])+([oym]-[oyear]*100)+(([oyear]-[ryear]-1)*12)) as [lifemonth],if(RowNo()=1,[countym],if(ccode<>Peek(ccode),[countym],if(ccode = Peek(ccode) and Peek([ordermonth]) = 1, 2, [countym]+Peek([ordermonth])))) as [ordermonth]Resident [count]Order By [ccode],[oym];Drop Table [count];生成數據表如下圖:
?
(8) 基于第七步生成的累計下單月數 [ordermonth] 和 注冊至今月數 [lifemonth],計算客戶在每個下單年月的忠誠度 [loyal%]
[count_final]:Load*,Num([ordermonth]/[lifemonth],'#,##0%') as [loyal%]Resident [count_temp];Drop Table [count_temp];Rename Table [count_final] to [count];生成數據表如下圖:
?
這樣就得到了每個客戶在每個下單年月的忠誠度。
筆者也在Qlik Community 投稿了英文版,有興趣請戳此鏈接
總結
以上是生活随笔為你收集整理的一种在Qlik Sense 中计算两个年月所覆盖时间跨度的方法以及场景应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我讽刺中医把每个养生的都化成了神仙
- 下一篇: HFUT雨课堂形式与政策【支持考试】