graph - 电源 BI : Make a line chart continuous when source contains null values (handle missing values)

标签 graph charts powerbi dax

这个问题衍生出了我之前发布的一个问题;
Custom x-axis values in Power BI

假设以下数据集:

enter image description here

专注于第二排和第三排。我怎样才能使下面相应图表中的线是连续的而不是停在中间?

enter image description here

在 Excel 中,我曾经通过在为图形生成数据的公式中应用 NA() 来解决这个问题。是否有使用 DAX 的类似解决方案?

最佳答案

简短版本:

你应该怎么做:

Unleash python然后按照那里的步骤,插入这个脚本(别担心,我在这里也添加了一些细节):

import pandas as pd

# retrieve index column
index = dataset[['YearWeek_txt']]

# subset input data for further calculations
dataset2 = dataset[['1', '2']]

# handle missing values
dataset2 = dataset2.fillna(method='ffill')

然后,您将能够像这样设置它:

enter image description here

为什么要这样做:

对于内置选项,据我所知,这些是您的选择:

1. 分类 YearMonth 和分类 x 轴不起作用

enter image description here

2. 分类 YearMonth 和连续 x 轴不起作用

enter image description here

3. 数字 YearMonth 和分类 x 轴不起作用

enter image description here

4. 数字 YearMonth 和连续 x 轴不起作用

enter image description here

详细信息,从内置方法失败的原因开始:

1. 分类年月和分类 x 轴

我使用了类似于您的表格屏幕截图的以下数据集:
YearWeek   1     2
201603     2.37  2.83
201606           2.55
201607     2.98 
201611     2.33  2.47
201615     2.14  2.97
201619     2.15  2.02
201623     2.33  2.8
201627     3.04  2.57
201631     2.95  2.98
201635     3.08  2.16
201639     2.50  2.42
201643     3.07  3.02
201647     2.19  2.37
201651     2.38  2.65
201703     2.50  3.02
201711     2.10  2
201715     2.76  3.04

不,我没有费心手动复制您的所有数据。只是你的 YearWeek 系列。其余的是 2 到 3 之间的随机数。

然后我将数据设置为 1 和 2 的数字,并在 Power Query 编辑器中将 YearWeek AA 类型文本设置为:

enter image description here

所以这是原始设置,带有像您这样的表格和图表:

enter image description here

数据按 YearWeek_txt 降序排列:

enter image description here

x轴设置为Categorical :

enter image description here

结论:失败

2. 分类年月和数字 x 轴

使用与上面相同的设置,您可以尝试将 x 轴类型更改为 Continuous :

enter image description here

但正如您将看到的,它只是立即翻转回“分类”,大概是因为 YearWeek 的类型是 text .

结论:失败

3. 数字 YearMonth 和分类 x 轴

我已经复制了原始设置,因此我有两个表 Categorical 和 Numerical 其中 YearWeek 的类型是 textinteger , 分别:

enter image description here

所以数字 YearMonth 和分类 x 轴仍然给你这个:

enter image description here

结论:失败

4. 数字 YearMonth 和连续 x 轴不起作用

但是现在,使用与上面相同的设置,您可以将 x 轴类型更改为 Continuous :

enter image description here

你最终会得到这个:

enter image description here

结论:哈哈

现在,Python:

Power Query Editor ,激活Categorical表,选择 Transform > Run Python Script并在运行 Python Script Editor 中插入以下代码段:
# 'dataset' holds the input data for this script

import pandas as pd

# retrieve index column
index = dataset[['YearWeek_txt']]

# subset input data for further calculations
dataset2 = dataset[['1', '2']]

# handle missing values
dataset2 = dataset2.fillna(method='ffill')

点击OK并点击 Tabledataset2 旁边这里:

enter image description here

你会得到这个(确保列数据类型正确):

enter image description here

如您所见,没有更多的缺失值。 dataset2 = dataset2.fillna(method='ffill')已将所有缺失值替换为两列中的前一个值。

单击关闭并应用以返回桌面,并享受您的表格和图表,不再缺少缺失值:

enter image description here

结论: Python 很酷

尾注:

这里有很多细节可能会出错,比如小数点、数据类型等等。让我知道你的情况如何,如果它对你不起作用,我会再看一遍。

关于graph - 电源 BI : Make a line chart continuous when source contains null values (handle missing values),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51402676/

相关文章:

charts - Chart.js和长标签

javascript - 从数组值中获取值以形成聊天

c# - 如何在 C# 中禁用图表控件的缩放?

powerbi - 在 Vega-Lite (Deneb) 中设置动态域

pdf - SAS:如何使用 ODS 布局在 2 个 PDF 页面上放置 8 个图形?

c# - Excel 工作簿和图表

python - 基于标签的共现图像聚类

algorithm - 非平面图中是否存在最小生成树?

sql - 使用直接查询模式在 Azure SQL 数据库中使用存储过程

angular - 使用 Power REST API - 获取组中的页面时,有没有办法从报告中过滤掉 "Hidden"页面