Split the API URL to Base, relative and Query - powerbi

Issue with PowerBi service failed
Dataset includes a dynamic data source. Since dynamic data sources aren't refreshed in the Power BI service, this dataset won't be refreshed. Learn more: https://aka.ms/dynamic-data-sources.
How to divide this url to basic , relative and query
"https://api.com/data/v1/shift & "?page=" &
Number.ToText(i) & "&limit=10000"
"
My solution :
Base Url : https://api.com/data/v1/
Relative Path : shift

In PowerQuery replace this expression:
Web.Contents("https://api.com/data/v1/shift" & "?page=" & Number.ToText(i) & "&limit=10000")
with that one:
Web.Contents(
"https://api.com/data/v1",
[
RelativePath = "shift",
Query =
[
page = Number.ToText(i),
limit = "10000"
]
]

Related

How to setup a stored procedure with incremental dates in Power BI

I am new to Power BI. I am using an SQL stored procedure to get the data.
execute ED_DS_TRANS_DETAIL01 #DateFrom ='2022-09-20', #DateTo = '2022-09-20'
It does give me the required data but I want it to be incremental. For example, today is the 20th and tomorrow is the 21st. So I want to set up the power bi in such a way that it gets the 21st data and then the next data 22nd and so on. Also, it doesn't replace the previous date data and places the next data underneath the previous one.
I have tried the given solution
let
Source = (Query as text) => let
Source = Sql.Database("IP", "DB" , [Query=Query, CreateNavigationProperties=false])
in
Source
in
Source
let
tdy = Date.From(DateTime.LocalNow()),
yest = Date.AddDays(tdy , - 1),
sQuery = Table.FromRecords({
[sQuery = "execute ED_DS_TRANS_DETAIL01 #DateFrom ='" & Date.ToText(yest,[Format="yyyy-MM-dd"]) & "', #DateTo = '" & Date.ToText(tdy,[Format="yyyy-MM-dd"]) & "'"
]}),
#"Invoked Custom Function" = Table.AddColumn(sQuery, "Query2", each #"Fnc Query"([sQuery]))
in
#"Invoked Custom Function"
GUI
When I click "OK" button I am getting Details: "Microsoft SQL: Incorrect syntax near '='."
You can do it at the Power Query side...
First create the parameters of dBaseIP and dBase in case you may need to change it later...
Then create a custom function which will run the SQL query "New Query" → Other Sources → "Blank Query".
let
Source = (Query as text) => let
Source = Sql.Database(dBaseIP, dBase , [Query=Query, CreateNavigationProperties=false])
in
Source
in
Source
At last, create the below function which will update your query as per today's date...
let
tdy = Date.From(DateTime.LocalNow()),
yest = Date.AddDays(tdy , - 1),
sQuery = Table.FromRecords({
[sQuery = "execute ED_DS_TRANS_DETAIL01 #DateFrom ='" & Date.ToText(yest,[Format="yyyy-MM-dd"]) & "', #DateTo = '" & Date.ToText(tdy,[Format="yyyy-MM-dd"]) & "'"
]}),
#"Invoked Custom Function" = Table.AddColumn(sQuery, "Query2", each #"Fnc Query"([sQuery]))
in
#"Invoked Custom Function"
Check this sample file... Change the database IP and name and then try to run it...

PowerBI how to combine values from 2 tables into a URL dynamically?

Using some sample data, I have table1 with the following values:
Name
Bob
John
Mary
and table 2 with the following values:
folder
documents
pictures
videos
I need to select one value from each table to build a URL. the final URL would be something like:
"https://example.com/Bob/Documents"
"http://example.com/Mary/Pictures"
The person viewing the report needs to choose a name and a folder, then click on a button to load the url.
I haven't been able to do this yet. I can do a dynamic column and get the name, but can't make it get the folder.
Folder table can be rebuild in another way if it solves the problem. Name table comes from Sharepoint and can't be altered.
thanks
You can create a measure using the below DAX, and if it helps then please consider Accept it as the solution.
Measure 5 =
VAR X = SELECTEDVALUE(Tabl11[Name])
VAR Y = SELECTEDVALUE(Table2[Folder])
RETURN "https://example.com/" & X & "/" & Y

Power Bi embedded - Dynamic Filter on report

'const filter = {
$schema: "http://powerbi.com/product/schema#basic",
target: {
table: "Geo",
column: "Region"
},
operator: "In",
values: ["West"]
};'
Is there any option/function/api to get Fields (table names and columns) used in a Power BI report?
Basically, I want to apply dynamic filters on reports in Power BI embedded, where I need to pass the table name and column name in filter object. Kindly help.
This can be achieved using Power BI report authoring library.
Follow the given steps to filter the visuals by the Fields (table name and column name):
Create an authoring page
Set the authoring page as active
Create a visual on the authoring page with required data fields
Get the data fields of the visual using .getDataFields()
ex- dataFields = visual.getDataFields('Data_Field_Name').
Create a filter and add the table name and column name from dataFields
Use updateFilters to update the filter the visual
You can try the above steps in Power BI Playground:
https://playground.powerbi.com/dev-sandbox (Go to Authoring section in the Playground first)
Please find the references below:
https://learn.microsoft.com/javascript/api/overview/powerbi/create-add-visual#how-to-create-a-visual-and-bind-it-to-data
https://learn.microsoft.com/javascript/api/overview/powerbi/data-fields#get-data-fields
https://github.com/microsoft/powerbi-report-authoring

Push Dataset in Power BI

I'm looking for a sample code which get data from SQL Server and push this to PowerBI in real time, This is basically using the Push Dataset option.
I am not sure how to Push the datas from SQL
Thanks
Why not creating a custom streaming dataset and 'pushing' your sql data directly. In this case you may use either Power apps (create a flow and a trigger on insert) or simply right some code to push your data in a form of a post request.
For instance you have your sql table containing a value you want to push. Thus the steps should the following:
Create a dashboard
Add tile
Choose 'Custom Streaming Dataset' as a source
Define the data colums to be pushed (for instance train_number and departure_time)
Copy the API
From your code (Python for example) get the data, convert it to json and publish
Go back to power bi, add a tile from newly created streaming dataset and chose the visual type. Important: the visuals are quite limited
Here is a sample code in python:
def data_generation(counter=None):
# get your SQL data and save it into 2 variables (row by row)
return [train_number, departure_time]
while True:
data_raw = []
# simple counter increment
counter += 1
for i in range(1):
row = data_generation(counter)
data_raw.append(row)
# set the header record
HEADER = ["train_number", "departure_time"]
# generate a temp data frame to convert it to json
data_df = pd.DataFrame(data_raw, columns=HEADER)
# prepare date for post request (to be sent to Power BI)
data_json = bytes(data_df.to_json(orient='records'), encoding='utf-8')
# Post the data on the Power BI API
req = requests.post(PowerBI_REST_API_URL, data_json)
print("Data posted in Power BI API")
print(data_json)
# wait 5 seconds
time.sleep(5)
Microsoft published similar walk-through. It has to be slightly expanded with SQL Server calls though:
Push data into a Power BI dataset
---> Create Dataset
You can't 'push' data from SQL, but you can use DirectQuery instead of Import. Then your data will always be actual.
Just connect to a SQL Server, and choose for 'Direct Query' and you'll be ready to go.
Edit:
With #Alexander Volok, of course, with an application and/or API calls you can push data into Power BI. My bad.
You Can push the data by using power shell which where you need to add the your api link and you have to put your sql connection string and you and you ca fire a query to same data set by declaring it into code you can refer the below code which will help you to understand how to push the data into your data set once you run your power shell script then data will be pushed to power bi data set and you can see your live
$SqlServer = ''; #your server name
$SqlDatabase = ''; #your database name
$uid ="" #User id
$pwd = "*****" # your password
$SqlConnectionString = 'Data Source={0};Initial Catalog={1};Integrated Security=SSPI;uid=$uid;Password=$pwd' -f $SqlServer, $SqlDatabase;
$SqlQuery = "SELECT * FROM abc;";
$SqlCommand = New-Object System.Data.SqlClient.SqlCommand;
$SqlCommand.CommandText = $SqlQuery;
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection -ArgumentList $SqlConnectionString;
$SqlCommand.Connection = $SqlConnection;
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCommand
$SqlConnection.Open();
$SqlDataReader = $SqlCommand.ExecuteReader();
##you would find your own endpoint in the Power BI service
$endpoint = "" ## add your api link middle of endpoint ""
#Fetch data from your table and write out to files
while ($SqlDataReader.Read()) {
$payload =
#{
"Date" =$SqlDataReader['Date']
"First Name" =$SqlDataReader['Name']
"Production" =$SqlDataReader['prdt']
}
Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json #($payload))
}
$SqlConnection.Close();
$SqlConnection.Dispose();
## every time you run script data will automaticaly pushed from sql server to your power bi report
e streaming chart

How can I use current datetime as parameters in a power bi query statement for REST API?

How can I use current datetime as parameters in a power bi query statement for REST API? I specified two parameters "DateStart" and "DateEnd" which I want to include in my Data source's SQL statement,i want to add DateEnd as current system datetime & DateStart as 15 min past datetime.
my query is
let
body = Text.ToBinary("{
""Type"": ""Feedbacks"",
""FromDate"": ""01-01-2015 23:00:00.000"",
""ToDate"": ""20-09-2017 23:00:00.000"",
""SearchField"": ""test"",
""SearchFieldValue"": ""*""
}"),
actualUrl = "http:/xx.xx.xx.xx:xx/service/GetSomething",
options = [
Headers =[#"Content-type"="application/json"],
Content=body
],
result = Web.Contents(actualUrl, options),
#"Imported JSON" = Json.Document(result,65001)
in
#"Imported JSON"
Here i want to add end date as current datetime & start date as current-15 min
Thanks in advance.