power bi排序
In this article, I’m going to demonstrate how to sort months chronologically in Power BI. Visualizing sales data in a time period is one of the most influential ways of reporting. Often, at times it is essential that you design charts that show the trend or growth of a metric over time. The time period can be anything for example days, weeks, months, or years. It basically gives you the idea of how the metric has increased or decreased over the specific period.
在本文中,我将演示如何在Power BI中按时间顺序对月份进行排序。 可视化一段时间内的销售数据是最有影响力的报告方式之一。 通常,有时必须设计图表以显示度量标准随时间的趋势或增长。 时间段可以是任何时间,例如几天,几周,几个月或几年。 基本上,您可以了解指标在特定时期内如何增加或减少。
In Power BI, you can also visualize your metrics by a specific selected period. This is usually implemented by drawing line charts that show the trend over time, or by using vertical bar charts that show specific metrics in the time period, or even simply using a tabular matrix to display the results textually. For the sake of this article, we will consider only the third scenario i.e. tabular matrix for simplicity. However, the same solution can be made to work for any type of chart which contains a time period in it.
在Power BI中,您还可以按特定的选定时期可视化指标。 这通常是通过绘制显示随时间变化趋势的折线图,或使用显示时间段内特定指标的垂直条形图,甚至只是使用表格矩阵以文本形式显示结果来实现的。 为了本文的目的,为了简单起见,我们将仅考虑第三种情况,即表格矩阵。 但是,对于包含时间段的任何类型的图表,都可以使用相同的解决方案。
Sometimes when you import data into Power BI, and specifically if you have months or quarters available as a textual data in the original data source (from a flat-file), then Power BI cannot understand whether the imported field is an actual date field (days/month/year, etc.) or just simple text data. In such a case, after the import, the months or quarters are sorted alphabetically, rather than chronologically which is an error depending on the requirements and something not so relevant in developing trend reports. In this solution, we will first learn how to reproduce the error, and then see how to make the necessary changes in the data model to resolve this error and sort months chronologically in Power BI.
有时,当您将数据导入Power BI时,特别是如果原始数据源( 来自平面文件 )中的文本数据可用于月份或季度,则Power BI无法理解导入的字段是否为实际日期字段(天/月/年等)或仅是简单的文本数据。 在这种情况下,导入后,将按字母顺序而不是按时间顺序对月份或季度进行排序,这是一个错误,具体取决于需求以及与开发趋势报告无关的东西。 在此解决方案中,我们将首先学习如何重现错误,然后了解如何在数据模型中进行必要的更改以解决此错误并在Power BI中按月顺序排序。
Let us first try to reproduce the error and then I shall explain how to sort the months chronologically in Power BI. In order to replicate the error, we need to create a simple CSV file as shown in the figure below.
让我们首先尝试重现该错误,然后我将解释如何在Power BI中按时间顺序对月份进行排序。 为了复制错误,我们需要创建一个简单的CSV文件,如下图所示。
Figure 1 – CSV Dataset
图1 – CSV数据集
As you can see in the figure above, we just have two simple columns – Month and Sales. The Month lists all the values starting from “January” to “December” and corresponding Sales values along with it. Once you have created the CSV file, the next step is to open Power BI and connect it to this dataset.
如上图所示,我们只有两个简单的列-Month和Sales 。 本月列出了从“一月”到“十二月”开始的所有值以及相应的销售值。 创建CSV文件后,下一步是打开Power BI并将其连接到此数据集。
Open Power BI Desktop and follow the steps below to fetch this data into the Power BI data model:
打开Power BI Desktop,然后按照以下步骤将这些数据提取到Power BI数据模型中:
Select on Get Data and select Text/CSV from the menu that appears
选择获取数据,然后从出现的菜单中选择文本/ CSV
Figure 2 – Get Data in Power BI
图2 –在Power BI中获取数据
Browse for the file that you just created in the previous step and click on Open
浏览您在上一步中刚创建的文件,然后单击“ 打开”
Figure 3 – Browse for Dataset
图3 –浏览数据集
In the next dialog that appears, verify the data and click on Load
在出现的下一个对话框中,验证数据并单击“ 加载”。
Figure 4 – Load data into Power BI Model
图4 –将数据加载到Power BI模型中
In the new table that is created, you can see that the months are now sorted alphabetically
在创建的新表中,您可以看到月份现在按字母顺序排序
Figure 5 – Creating the table
图5 –创建表
As you can see in the figure above, when you drag and drop the Month field into the Values section of the table, all the months are sorted but alphabetically. However, in usual reference, we often tend to analyze months or rather any periods chronologically and not alphabetically. So, in order to sort months chronologically in Power BI, we would need to make some transformations in the data model. These transformations can be done using the Power Query Editor that is available within the Power BI Desktop tool.
如上图所示,将Month字段拖放到表的Values部分中时,所有月份均按字母顺序排序。 但是,在通常的参考中,我们通常倾向于按时间顺序而不是按字母顺序分析月份或任何时期。 因此,为了在Power BI中按时间顺序排序几个月,我们需要对数据模型进行一些转换。 可以使用Power BI Desktop工具中提供的Power Query Editor完成这些转换。
Now that we know what the actual error is all about, let’s go ahead and make the necessary changes in the data model to sort the months chronologically in Power BI. The idea is to create a dummy date value based on the Month data that is available in the dataset. Once the date values are available, we can just extract the Month Number and sort the Month column using the Month Number field. You can follow the steps provided below.
现在我们知道了真正的错误所在,让我们继续进行数据模型的必要更改,以便在Power BI中按时间顺序对月份进行排序。 这个想法是基于数据集中可用的月份数据创建一个虚拟日期值。 一旦日期值可用,我们就可以提取“ 月号”并使用“ 月号”字段对“ 月”列进行排序。 您可以按照下面提供的步骤进行操作。
In the Power BI Desktop, select Transform Data and then click on Transform Data
在Power BI桌面中,选择“ 转换数据” ,然后单击“ 转换数据”
Figure 6 – Transform Data
图6 –转换数据
In the formula for the custom column, use the following: = “1 ” & [Month] & ” 2020″
在自定义列的公式中,使用以下命令: =“ 1”和“ [月]”和“ 2020”
Figure 7 – Adding Dummy Date Column
图7 –添加虚拟日期列
So basically, what we are trying to achieve is just create a dummy date value by adding “1” as the date and “2020” as the year value to the month that already exists
因此,基本上,我们想要实现的就是通过将“ 1”作为日期,将“ 2020”作为年值添加到已经存在的月份来创建一个虚拟日期值
Figure 8 – New Date Column Added
图8 –添加的新日期列
The next step is to convert this new field, Date to a date datatype. Right-click on the column and select Change Type and then select Date
下一步是将这个新字段Date转换为date数据类型 。 右键单击该列,然后选择“ 更改类型” ,然后选择“ 日期”
Figure 9 – Changing the Data Type
图9 –更改数据类型
You can see that the data type and values for this column have changed
您可以看到此列的数据类型和值已更改
Figure 10 – Changed the Data Type
图10 –更改了数据类型
Provide the name of the field as “MonthNumber” and formula as “= Date.Month([Date])” and click OK
提供字段名称为“ MonthNumber ”和公式为“ = Date.Month([Date])”,然后单击“ 确定”。
Figure 11 – Adding new MonthNumber Column
图11 –添加新的MonthNumber列
You’ll now see one more column added in the dataset
现在,您将在数据集中看到另外一列
Figure 12 – Added new MonthNumber Column
图12 –添加了新的MonthNumber列
We will change the data type of this field to the Whole Number. Right-click on the MonthNumber, select Change Type and then select the Whole Number
我们将此字段的数据类型更改为“整数”。 在MonthNumber单击鼠标右键,选择更改类型 ,然后选择整数
Figure 13 – Change the Data Type to Whole Number
图13 –将数据类型更改为整数
Since we have the desired field MonthNumber in our data model, we can remove the dummy date field that we created in our previous steps. Right-click on the Date column and select Remove
由于我们的数据模型中具有所需的字段MonthNumber ,因此我们可以删除在先前步骤中创建的虚拟日期字段。 右键单击“ 日期”列,然后选择“ 删除”
Figure 14 – Removing the dummy Date column
图14 –删除虚拟日期列
Finally, you can close the Power Query Editor window by navigating to the Home and select Close and Apply
最后,您可以导航至主页,然后选择“ 关闭并应用”,以关闭“ Power Query Editor”窗口。
Figure 15 – Closing the Power Query Editor
图15 –关闭电源查询编辑器
Now that we have made the necessary changes in our data model to sort months chronologically in Power BI, the final step is to set the sorting order on the Month in ascending order of MonthNumber. Please follow the steps below to sort the months.
现在,我们已经对数据模型进行了必要的更改,以便在Power BI中按时间顺序对月份进行排序,最后一步是将MonthNumber的升序设置为Month上的排序顺序。 请按照以下步骤对月份进行排序。
In the drop-down that appears, select MonthNumber and navigate to the Reports tab
在出现的下拉菜单中,选择MonthNumber并导航至“ 报告”标签
Figure 16 – Sort Month By Month Number
图16 –按月份编号排序月份
You can now see, that the months are being sorted chronologically as opposed to alphabetically
您现在可以看到,月份是按时间顺序而不是按字母顺序排序的
Figure 17 – Months sorted chronologically in Power BI
图17 –在Power BI中按时间顺序排序的月份
Additionally, if you want to hide the MonthNumber field, you can just right-click on it and select Hide.
此外,如果要隐藏MonthNumber字段,可以右键单击它,然后选择Hide 。
Figure 18 – Hide MonthNumber field
图18 –隐藏月号字段
You’ll have your dataset as was in the original CSV file but the months are sorted chronologically.
您将拥有原始CSV文件中的数据集,但是月份按时间顺序排序。
Figure 19 – Months Sorted Chronologically in Power BI
图19 – Power BI中按时间顺序排序的月份
In this article, I have explained how to sort months chronologically in Power BI. I have also mentioned the steps to replicate the error and then provided step-by-step guidance on how to resolve the issue and finally get the desired results.
在本文中,我解释了如何在Power BI中按时间顺序对月份进行排序。 我还提到了复制错误的步骤,然后提供了有关如何解决问题并最终获得所需结果的逐步指导。
翻译自: https://www.sqlshack.com/how-to-sort-months-chronologically-in-power-bi/
power bi排序