Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format timeseries data for a matplotlib visualization in Power BI

As of today, August 9 2018, Power BI supports Python Visualizations. They've had support for R Visualizations before, but I still find these integrations to be a bit awkward. Let me show you what I mean:


Let's say that you have a table with time series data, where the top row containts the names 'Date' and 'Value', and the contents are dates of the form yyyy-mm-dd and a number, respectively:

Date,Value
2017-01-12,1
2017-01-13,4
2017-01-14,2
2017-01-15,4
2017-01-16,2
2017-01-17,2
2017-01-18,2
2017-01-19,5
2017-01-20,5
2017-01-21,5
2017-01-22,5
2017-01-23,6
2017-01-24,3
2017-01-25,6
2017-01-26,6
2017-01-27,5
2017-01-28,8
2017-01-29,4
2017-01-30,2

If you store that dataset as a textfile like timerseries.csv and import it using Get Data | Text/CSV, you get a table uner VISUALIZATIONS | FIELDS, like this:

enter image description here

You can inspect your table using VISUALIZATIONS | Table and get:

enter image description here

With this setup, one should think that you were all set for unleashing the power of a Py VISUALIZATION using this beautiful new feature:

enter image description here

If you click that, you get this:

enter image description here

And you're told to

Drag fields into the Values area in the Visualization pane to start scripting

If you start with Value, you get this default setup in the editor:

enter image description here

And if you follow the instructions given by the Power BI team in the August 2018 feature summary you should be able to make a matplotlib plot quite easily.

enter image description here

But this is where it ends for me at the time being.

If the default dataframe in the editor shares the features of a standard dataframe, you should be able to reference a column in that dataframe and easily make a plot with this snippet:

import matplotlib.pyplot as plt
plt.plot(dataset['Value'])
plt.show()

But when you run it, it onlu returns an error:

enter image description here

And the details are elaborate to say the least.

I've also tried to import both Dates and Values, and I've tried plotting the dataframe directly with dataset.plot(), but nothing seems to be working. I've also tried stripping the date hierarchy down to simple dates this way:

enter image description here

So, any ideas on the dataformat, import method and/or the snippet?

Thank you for any suggestions!

EDIT 1 - Following the answer from Foxan Ng:

Add both columns in the Value field:

enter image description here

This still returns an error edning with:

TypeError: from_bounds() takes 4 positional arguments but 6 were given

like image 698
vestland Avatar asked Sep 11 '25 02:09

vestland


1 Answers

I didn't encounter errors that you've mentioned. Have you dropped in both columns into Values?

import matplotlib.pyplot as plt
plt.plot(dataset['Date'], dataset['Value'])
plt.show()

images


UPDATED with M query:

let
    Source = Csv.Document(File.Contents("C:\your-directory..\timerseries.csv"),[Delimiter=",", Columns=2, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"Value", Int64.Type}})
in
    #"Changed Type"

timeseries

like image 161
Foxan Ng Avatar answered Sep 13 '25 15:09

Foxan Ng