LOD | 在低層級上聚合高層級的聚合數據

舉例,通過LOD找到每個州產品類別中具有最大品類的是什么

如下可以看到,加利福尼亞的Paper是最多的品類。

如下的例子,在不使用任何字符串操作的前提下,驗證LOD的作用,在每個州中,找到最大的品類

Here’s the algorithm I use for this kind of calculation,這里我針對這種類似的案例使用這一類方式 I use this pattern pretty regularly這種模式也是比較通常的 ?becausemost of my work is done in MS Access? 因為我的大部分工作是在MS的Access中完成的?where we don’t have more advanced features like partitioning and row numbering functions.Access中沒有過多的高級特性,比如數據分區(qū)功能,計數功能

使用conutd()去取得每個品類和每個州的用戶ID計數,是非重復計數,所以取得的是人數。

在Access中,是通過稍微復雜一點的方法,一系列聚合子查詢(Access并沒有類似count的函數)

但是呢,在tableau中就簡單了,這就是LOD。重復一下,不僅僅是對用戶計數,而是基于州的用戶計數,和基于產品類別的計數。

對于類別和州這兩類指標,取得每個州最大的。而這個是Access是一列操作,在tableau中是個LOD表達式。

在類別和州的層級上,將步驟1和步驟2的結果組合到一起。這個操作在Access中需要其他的輔助查詢。tableau這是LOD表達的內置功能。

在Access中,寫一個計算,判斷兩組數據是否匹配,然后再返回對應的類別信息。這在Access中使用IIF函數實現,同時在tableau中對應的是IIF or IF

然后返回這些結果,就可以得到最終的數據了。

但是,這仍然是復雜的,待我慢慢講來。

如果你寫的一個SQL的聚合計算查詢,已經有充足的經驗將大塊的數據分解成更小的顆粒,直到每個小的顆??梢栽陬A定的層級做到單獨的計算

And when working with SQL as part of that you’ve had practice in imagining what the query is doing prior to seeing the results,?

當使用SQL的一部分,你已經練習想象查詢在看到結果之前,

你在工作的時候使用SQL作為一部分功能,同時想象著這個查詢到底在做啥,

and (if you’re at all like me) have had tons of practice in figuring out where things went wrong when the granularity of the data vis-a-vis the GROUP BY fields, joins,

并且(如果你也是像我一樣)有過許許多多經驗in解決出錯,在顆粒分組的時候。

and when calcs were performed made too many rows, too few rows, or just plain wrong results.

當計算進行了太多的行,過幾行,或是錯誤的結果。

更甚,當計算執(zhí)行后,有n多的行列,n多的報錯,那么我想你是崩潰的

Where LOD expressions getbrain-twistingly complicatedfor people who haven’t had experience writing tons of SQL aggregate queries is that they haven’t had that practice in breaking things down,?

在LOD表達getbrain歪歪扭扭的complicatedfor人沒有寫SQL查詢噸的經驗是,他們沒有打破的東西有實踐,

LOD是給誰用的呢?LOD曲解難懂,給那些人,沒有太對SQL聚合查詢經驗

*plus* in Tableau the aggregations that LOD expressions compute are only visible as results.

*加*畫面LOD表達計算是唯一可見的結果聚合。

LOD的聚合計算只在結果是可見的,你不用關注過程。


創(chuàng)建并驗證LOD,我所使用的LOD,為了使LOD的實現過程更清晰,我在每個step都做了對應的表達式,用以對比驗證。


1 首先將想要使用的維度,拖入視圖。此案例中,我希望使用的都是“州”這個字段

拖入視圖,意思是將該維度放入到任何地方,行、列、頁面、標記,都行的

但是篩選器不包括哦,因為篩選器并不能改變視圖聚合的維度或顆粒。


2 拖入其他的字段,構建一個基本的視圖(不含LOD的哦)

看圖,拖入了“州”,以及custom ID的計數

3 對于每個LOD的表達式,指出對應需要的維度


注意:

FIXED,只能用于這種情況,就是LOD使用固定維度層級得字段,不會依據Viz視圖情況而改變。

INCLUDE & EXCLUDE,可以用來加在任何維度的后面,也可以放在任何其他的嵌套表達式內層中。

關于篩選器的優(yōu)先級

? ? 篩選器也是優(yōu)先級順序的,(如下圖)從上到下的順序為:

FIXED>維度篩選器>INCLUDE/EXCLUDE

FIXED的表達式,是across all the data的,不用考慮篩選器的影響

所以,如果需要對FIXED表達式做篩選,就只能采用上下文篩選器(Context)


這工作會花費一些精力,不過為了做到精確的結果,這個絕對必要的。

就像我們經常做的任何事情一樣,在實踐中,這可以成為第二天性。

在mark'supdatehe的描述了Joe Mako的當前使用的技術,就是使用電子表格列出維度和度量,然后使用顏色區(qū)分INCLUDE和EXCLUDE的區(qū)別。

在這個例子中,唯一的附加維度是類別,所以維度是狀態(tài)和類別。

4 使用對應的維度,開始一個LOD的視圖


然后將LOD拖入到視圖中,使用一種集合方式,比如SUM or AVG。

In the case of strings you can use MIN and MAX for validation. If each aggregation returns the exact same result,?

這個案例中,你可以使用min or max,如果每個聚合返回值都是同樣的結果,那么很大可能你使用了正確的維度。

then you can be mostly sure that you’ve got the right dimensionality in the LOD workout view.?

I write “mostly” because there are dependencies on the granularity of the data that crop up as you get into nested LOD expressions as well as the main view.

我之所以說是“很大可能”,是因為要依據情況而定,要考慮到嵌套LOD在主視圖的數據顆粒度,

這里寫第一個表達式,返回了用戶的非相同計數,基于“類別”和“州”

{INCLUDE [Category] : COUNTD([Customer ID])}

And here’s that calc in a view with State & Category as dimensions. We can see that COUNTD(Customer ID) is the same as the SUM, AVG, and MAX of our LOD calc:

如下就是兩個維度,類別和州,作為維度的展示??梢钥吹剑@種情形下countd和sum。avg,max是相同的

注意了,countd使用的就是普通聚合,其它三個使用的LOD(藍色框)

6 重復3-5步,嘗試不同的level

For example, if you are nesting LOD expressions at multiple levels of detail, that can mean putting the same calculation into multiple workout views to see what it’s returning.?

例如,如果你在多層次的情況下,使用嵌套LOD,這意味著可,使用同樣的計算在多重工作場景去查看數據返回值。


In order to validate what INCLUDE & EXCLUDE LOD calcs are returning to the data prior to the aggregation at the vizLOD, you can highlight marks and use right-click->View Data->click on Underlying tab to see what they are returning (there’s an example of this below).

為了證實兩者(INCLUDE和EXCLUDE)的在優(yōu)先級上的區(qū)別,可以使用格式編輯,用以區(qū)分二者的區(qū)別,下邊會有栗子

?Then you’ll also need to pay attention to how each LOD expression is aggregated in the different views.

在不同的view下,你還是要多多注意看,每個計算的結果。

In our case the only other granularity is that for the main view, the State, so we can skip this step.

我們的例子中,對于顆粒,我們只有兩個,類別和州,1個是類別,另一個就是州,只有以后遇到超過兩個類別的時候,再去特殊關注吧

7. Once you have the calcs working in the individual views, bring them one by one into the main workout view. Verify that they are returning the desired results.


一旦在單獨的視圖中開始使用計算,將給他們逐個的在主視圖中顯示,記得驗證一下結果是不是你要的結果,這個很重要。

吶,如下即是,在同樣的聚合條件下,主視圖中countd,非重復計數

此圖比前一個圖,去掉了類別字段


復習一下看下圖:紫色的是第二次引用的圖,藍色的是第一次引用的圖

區(qū)別是紫色(第二次)去掉了“類別”

藍色(第一次)保留了“類別”

紫色的15與,下圖的15是對應的


Since the goal is to return the max COUNTD(Customer ID) for each Category, the MAX(CountD per Category) works.

因為目標是返回每個類別最大Max用戶“非重復計數”

But we don’t want that to be returned as an aggregate in the view,

但是我們并不想看的是,返回一個聚合的數據

we want that as a record-level calc so a comparison can be made. The following Max CountD per Cat in State calc does that:

我們想知道的是對應級別的數據,這是做個對比是有意義的。下面的每個類別Max countd()就是做了這件事

吶,也就是上邊藍色圖的15余紫色圖的15啦

{INCLUDE : MAX([CountD per Category])}

公式如此

This uses a little trick in LOD calcs where by not including any specific dimensions we can force an aggregation at the level of detail of the viz (i.e. State) and still return that as a record-level result.

這里用了一個小技巧,不針對任何維度的計算,只寫了INCLUDE,而后邊不用跟具體維度,它是等效于INCLUDE?

額不知道怎么回答?

Here’s that calc in the workout view at different aggregations, showing it is returning the same result for each version:

Now for step 4 in the algorithm I laid out. Here’s the formula for the Largest Category (initial) calc:

這里,在增加一項目,如下是公式的細節(jié)(initial的)


IF [CountD per Category] == [Max CountD per Cat in State] THEN

[Category]

END)


意思是,如果 countd等于算出的Max值,那么久返回類別

否則就為空

估算一下結果,就是15那個,記得吧,只有最多的幾項會顯示對應的類別,其他的都為空

結果大概就是這個樣子咯



繼續(xù)


This calc takes advantage of a second trick with LOD calcs that I hadn’t known about until reviewing #8 in theTop 15 LOD Expressions.?

tableau官方有Top15個LOD應用,作者在讀到第8個例子之前,還是對第二招的技巧不太知道

While we can use the results of a FIXED LOD expression in the view as a dimension or aggregated measure, in the view we can only use INCLUDE & EXCLUDE LOD expressions as aggregated measures.?

然而,當我使用了FIXED LOD的結果,再嵌套IN or EX的方式,可以完成很多事情

*However*, just like FIXED LOD expressions the results of INCLUDE & EXCLUDE expressions are returned at a record-level *and* can be evaluated at a record-level.?

盡管如此,

So the IF statement evaluation is only True when for a given record the CountD per Category for that record’s Category is also the maximum value in that record’s State, and the calc returns the Category for those records.

所以,IF的狀態(tài)評價,是唯一的

而該計算返回這些類別的信息

Note that Tableau automatically applies the Count aggregation in the Data window, indicating that this calc has to be aggregated in some way in a view:

記住,tableau會在數據窗口中,自動的應用count的聚合

表明這個計算不得不被聚合以某種方式

看如下截圖

Because we’re using a mix of calcs meant to work at different levels of detail here, validating this gets a little tricky.?

因為我們使用了混合的計算方式,意味著我們工作在不同的level,同時證實了這個方式是個不錯的技巧。

We have to use the View Data->Underlying tab technique to see what’s going on, here I’ve selected Alabama:

現在,我們使用查看數據源,詳細信息,來看看數據怎么變化的。如下是我選擇了亞拉巴馬州的數據。


可以看到那個IF的計算起作用了,如我所說,等于Max的,顯示類別,不等于Max的顯示null。

結果顯示,Office Furnishings和Paper是最多的類別。

This is because there’s a tie with both having 15 distinct customers. If we wanted to show all the ties we’d run into a key difference between INCLUDE & EXCLUDE vs.?

這是因為有兩個達成平局了,他們都是15個Max

于是,如果我想show出所有的平局競爭者,我應該去找到三者的關鍵區(qū)別FIXED,(FIXED,INCLUDE,EXCLUDE這三個家伙的區(qū)別)

namely that INCLUDE & EXCLUDE LOD expressions *have* to be aggregated in a view, and since the view is at the level of detail of State that meants the aggregations of MIN(), MAX(), etc. all only return a single result, not every result.?



So there’s no way to use the INCLUDE & EXCLUDE to show the ties, I’ve got a follow-up post in the works that describes how an alternative calc using FIXED can show all the ties.

所以使用IN or EX 是沒辦法顯示tie在哪里,我后邊做了一個表,描述了,備選的計算方式使用FIXED可以顯示所有的tie


To resolve that tie for now, we’ll use the MIN() aggregation:


為了解決tie的問題,來試試min()的效果

看圖,在Row,“州”字段的右側有拖入一個min

To build the Largest Category (final) calc I chose to apply that aggregation in the calc (that way Tableau won’t be adding anything to the column name in the view) and we’ll add in the CountD per Category as part of as string to show that as well, so it will look like “Office Furnishings (15)” for Alabama. Here’s the formula:

下一步,創(chuàng)建final版本的Max類別計算字段,我選擇了在計算中應用聚合,(tableau在這個計算中并不會加入任何列名)并且我會添加countd計算,在每個類別中,作為xxx的部分,所以就是很像Office Furnishings (15)” for Alabama,如下是公式:

就是在IF的外層,嵌套了min()

在類別的右側加了個括號

括號內部寫的是實際的count值

MIN(IF [CountD per Category] == [Max CountD per Cat in State] THEN

[Category] + ' (' + STR([CountD per Category]) + ')'

END)


Here’s the calc when looking at the Underlying Data:

在源數據中的樣子


And in the view

在視圖中的樣子

Building the final worksheet is a matter of dragging and dropping pills to remove the unneeded measures and generate the final chart:

基于上邊的工作,可以著手創(chuàng)建最終的視圖了,簡單說,就是拖入有用字段,移除無用字段。

Why stop here, though? One key feature of using INCLUDE & EXCLUDE LOD expressions is that the results are dependent on the dimensions in the view.?

那么,為什么停在這里?

一個主要的區(qū)別,它們三者,就是,視圖中的結果會依據屬兔中的字段維度的不同,而不同的。

We can take advantage of that (and all the work we’ve done) and simply drag & drop dimensions. Here’s a one-click change to finding the largest category for each Container:

我們可以利用這個已經完成了測試視圖,做一些簡單的拖拉維度,即可完成最終視圖。

如下,是一個最終構圖的字段截圖,可以參考。


And we can have multiple dimensions, here I’ve added the YEAR(Order Date) to the view with one more click:

那么,我們有多個維度,我又添加了“年”到視圖中

For those readers who have experience using Tableau table calculations, this last bit is incredibly awesome.?

對于讀者,那些有經驗的讀者,使用過tableau的讀者,這最后一點是難以置信的nb

We could have built any of these views with table calculations, but switching out dimensions would likely break things so every new view would have to be hand-crafted and revalidated,?

我們可以制造任何這些表計算,但開關的尺寸可能會打破的東西,所以每一個新的視圖必須手工重新驗證,

whereas with LOD expressions we can be back in the flow of dragging & dropping pills to ask more questions of our data.

而,有了LOD的幫助,,我們可以再次回到思維流的狀態(tài)了,更多的關注數據的故事,而不是技術。

總結


?how to work with LOD expressions in a stepwise fashion so you can validate each step of the way, and demonstrated a couple of lesser-known aspects of LOD expressions: using INCLUDE or EXCLUDE without a dimension, using results of INCLUDE or EXCLUDE to do record-level evaluations, and finally how easy it can be (once you’ve done the heavy lifting) to rearrange views using LOD expressions. I hope it’s useful for you, if you have any comments, questions, or other tips, please comment below!


這篇文章描述了另一種算法,使用LOD表達式,從一個較低的水平返回一個維度(細粒度)到一個更高的粒度

如何在逐步時尚LOD表達工作這樣你就可以驗證每一步的方式,并展示了幾個鮮為人知的方面:使用LOD表達包括或排除不一個維度,使用結果包括或排除做記錄水平的評估,最后怎么可以輕易(一旦你完成繁重)重新采用LOD表達意見。我希望這對你有用,如果你有任何意見,問題或其他提示,請在下面評論!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容