简体   繁体   English

Google Data Studio 如何计算不同值的总和

[英]Google Data Studio how to calculate sum of only distinct values

I have data set which contains work hours and invoices related to these work hours.我有包含工作时间和与这些工作时间相关的发票的数据集。 Some work hours are not related to any invoices yet.某些工作时间尚未与任何发票相关。 Several work hours can be related to a single invoice.多个工作时间可能与一张发票相关。 Example of data:数据示例:

Date日期 Project项目 Hours小时 Price of hours小时价格 Invoice发票 Invoice total发票总额 Invoice hours发票时间 Invoice others开发票别人
2021-05-06 2021-05-06 Project 1项目一 7.5 7.5 500 500 invoice_id_1 invoice_id_1 1500 1500 1200 1200 300 300
2021-05-07 2021-05-07 Project 1项目一 7.5 7.5 500 500 invoice_id_1 invoice_id_1 1500 1500 1200 1200 300 300
2021-05-08 2021-05-08 Project 1项目一 7.5 7.5 600 600 invoice_id_2 invoice_id_2 600 600 600 600 0 0
2021-05-09 2021-05-09 Project 1项目一 2 2个 100 100 invoice_id_1 invoice_id_1 1500 1500 1200 1200 300 300
2021-05-10 2021-05-10 Project 1项目一 7.5 7.5 550 550 null null null null null null null null

I want to create 3 scorecards that shows the total sum of invoices, total sum of invoice hours and total sum of invoice others.我想创建 3 个记分卡,显示发票总金额、发票小时总金额和其他发票总金额。

However, if I simply have SUM(invoice_hours), the sum would in this case be 4200, but I would like it to be 1800 as three of entries are in the same invoice.但是,如果我只有 SUM(invoice_hours),在这种情况下总和将是 4200,但我希望它是 1800,因为三个条目在同一张发票中。 I could fix this by using Price of hours instead, but that doesn't help with Invoice others.我可以改用小时价格来解决这个问题,但这对其他发票没有帮助。

So my question is this: is it possible to count the sum of a field, counting rows where another field is not distinct only once?所以我的问题是:是否可以计算一个字段的总和,计算另一个字段不唯一的行一次? So in this case only counting the first row and third row (ignoring rows 2 and 4 as the invoice_id_1 has already been counted).所以在这种情况下,只计算第一行和第三行(忽略第 2 行和第 4 行,因为 invoice_id_1 已经被计算在内)。

For what it's worth, I'm using a community connector that I created that fetches the data from an API, so I can modify the data format / add fields if it's necessary.对于它的价值,我正在使用我创建的社区连接器,它从 API 获取数据,因此我可以在必要时修改数据格式/添加字段。

There is the need to remove duplicated.有必要删除重复项。 This cannot be done in Data Studio.这无法在 Data Studio 中完成。 It would be an easy task for BigQuery (select distinct Invoice, Invoice_total from ...).对于 BigQuery 来说,这将是一项简单的任务(从...中选择不同的 Invoice、Invoice_total)。

Maybe you can do this as well in your community connector?也许您也可以在社区连接器中执行此操作?

If further filtering of the data is not possible at the moment ( ie: being unable to blend already blended data ) and there are few different invoice IDs then a workaround could be, albeit tedious:如果目前无法进一步过滤数据(即:无法混合已经混合的数据)并且几乎没有不同的发票ID,那么可能会有一个解决方法,尽管很乏味:

sum_invoiceHours : sum_invoiceHours

SUM( IF( invoice = "invoice_id_1", invoice_hours , null ) )
    / COUNT( IF( invoice = "invoice_id_1", invoice , null ) )
+ SUM( IF( invoice = "invoice_id_2", invoice_hours , null ) )
    / COUNT( IF( invoice = "invoice_id_2", invoice , null ) )

Which solves as:其解决为:

在此处输入图像描述

Now, to prevent unusual behaviours I'd recommend throwing in IFNULL() in each quotient:现在,为了防止异常行为,我建议在每个商中加入 IFNULL() :

IFNULL( SUM( IF( invoice = "invoice_id_1", invoice_hours , null ) )
    / COUNT( IF( invoice = "invoice_id_1", invoice , null ) ) ,0) 
+ IFNULL( SUM( IF( invoice = "invoice_id_2", invoice_hours, null ) ) 
    / COUNT( IF( invoice = "invoice_id_2", invoice , null ) ) ,0)
+ IFNULL( SUM( IF( invoice = "invoice_id_3", invoice_hours , null ) ) 
    / COUNT( IF( invoice = "invoice_id_3", invoice , null ) ) ,0)

... it's not ideal (nor pretty) but it'll get the job done for now . ...它不理想(也不漂亮),但它现在可以完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在速视中计算不同的总和 - How to calculate sum distinct in quicksight 如何将两个类别的值相加并显示在 Google Data Studio 的柱形图中? - How to sum up values of two category and show in a column chart in Google Data Studio? Google Data Studio - 如何将用户只能看到嵌入式仪表板而不是 go 的仪表板嵌入到 Data Studio 页面? - Google Data Studio - How to embed a dashboard that the user can only see the embedded dashboard and not go to the Data Studio page? 负值的 Google 数据洞察表热图 - Google Data Studio Table Heatmap for Negative values 如何计算当前值及其以下所有值的总和 - How to calculate sum for current value and all the values below it Google Studio Count Distinct 返回值错误 - Google Studio Count Distinct return with wrong value 如何在 Google Data Studio 中显示单个日期? - How to Display a single Date in Google Data Studio? 计算每个事件在 google data studio 中每 session 发生的平均次数 - Calculate the average times each event occurs per session in google data studio 如何对 firebase android studio 中的多个值求和 - how to sum multiple values from firebase android studio 如何将 Postgres 数据库连接到 Google Data Studio - How to connect Postgres DB to Google Data Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM