Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

Retrieve all Posted Sales Invoice Lines using API for Power BI

(0) ShareShare
ReportReport
Posted on by 50

Hello,

I need to do some analysis based on Posted Sales Invoice Lines in Power BI. However, I do not see way, which API endpoint should I use, trying to use latest version.

I have found out the URL - https://api.businesscentral.dynamics.com/v2.0/(mytenantid)/myenviornment/ODataV4/Company('mycompany')/salesDocumentLines

However, this API endpoint requires the document number is passed as parameter for particular one Sales Document.

I need to retrieve all lines from all posted sales Invoices.

Which API endpoint should I use?

Using Dynamics 365 Business Central v19.3 SaaS.

Thank you

  • Suggested answer
    wikap.dk Profile Picture
    wikap.dk 456 on at
    RE: Retrieve all Posted Sales Invoice Lines using API for Power BI

    Hi All in this thread (especially thanks to Hkusulja for starting this thread - I have been looking for long time for these discussions).

    To do prober and detailed Sales Reporting, I also have the need to exctact all salesInvoiceLines into a Power BI table. I don't want to go the BC Pages webservice way, since in a Danish BC the names changes and I like the new API 2.0 (except that I don't get custom fields without having a BC consultant writing a new custom API :( ).

    I have a lot of salesInvoiceLines and this made load times loooong for me, so I tried to do some extensive testing. CONCLUSION: the Business Central connector outperforrmed the oData connector, if you filter wisely. BUT check out my oData expant/select/filter options, which increased performance significantly and took me a lot of time to format correct (I can post my inspiration articles if you like).

    Hope you could use my tips until Microsoft makes salesInvoiceLines standard API able to get all lines instead of only one at a time

    Option 1: BUSINESS CENTRAL CONNECTOR

    • It loaded 92.000 rows in 3:38 minutes
    • I hate the idea in the Business Central connector that we cannot filter and select columns in the initial step (it load all data from the table as the Dataverse connector also do) 
    • Important to filter before you expand - this is crucial to performance!!

    let
    Source = Dynamics365BusinessCentral.ApiContents(null, null, null),
    XXXXX = Source{[Name="XXXXX"]}[Data],
    #"YYYYY" = XXXXX{[Name="YYYYY"]}[Data],
    v2.0 = #"YYYYY"{[Name="v2.0"]}[Data],
    salesInvoices_table = v2.0{[Name="salesInvoices",Signature="table"]}[Data],
    #"Renamed Columns" = Table.RenameColumns(salesInvoices_table,{{"id", "id_salesInvoice"}}),
    #"Removed Other Columns1" = Table.SelectColumns(#"Renamed Columns",{"id_salesInvoice", "postingDate", "salesInvoiceLines"}),
    #"Filtered Rows2" = Table.SelectRows(#"Removed Other Columns1", each [postingDate] >= #date(2021, 1, 1)),
    #"Expanded salesInvoiceLines" = Table.ExpandTableColumn(#"Filtered Rows2", "salesInvoiceLines", {"sequence", "itemId", "lineType", "quantity", "discountAmount", "discountPercent", "amountExcludingTax", "netAmount", "shipmentDate"}, {"sequence", "itemId", "lineType", "quantity", "discountAmount", "discountPercent", "amountExcludingTax", "netAmount", "shipmentDate"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded salesInvoiceLines", each ([lineType] = "Item")),
    #"Filtered Rows1" = Table.SelectRows(#"Filtered Rows", each [quantity] <> 0)
    in
    #"Filtered Rows1"

    Option 2: ODATA CONNECTOR:

    • It loaded 92.000 rows in 4:17 minutes after the following optimizations/adjustments
    • Using select only worked if I replaced [Implementation="2.0"] with null (otherwise it returned Error for all columns not selected making refreshing data fail)
    • My idea was to use select and filter to reduce columns and rows
    • Notice that I filter on both the salesInvoice table AND the expanded salesInvoice table
      • filtering on the expanded table only gave minor improvements in load time, and I'm not sure that this would always be a good idea
      • without 17 minutes!
    • Notice I could only make it work by including the $expand in the query (otherwise I got error from BC: 'OLE DB or ODBC error: [DataSource.Error] OData: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host'

    let
    Source = OData.Feed("api.businesscentral.dynamics.com/.../XXXXX/api/v2.0" & "/salesInvoices" & "?$select=id,number,postingDate,salesInvoiceLines" & "&$filter=postingDate ge 2021-01-01T00:00:00Z" & "&$expand=salesInvoiceLines($select=itemId,lineType,quantity,discountAmount,discountPercent,amountExcludingTax,netAmount,shipmentDate;$filter=lineType eq 'Item' and quantity ne 0)", null, null),
    #"Removed Other Columns" = Table.SelectColumns(Source,{"id", "number", "postingDate", "salesInvoiceLines"}),
    #"Expanded salesInvoiceLines" = Table.ExpandTableColumn(#"Removed Other Columns", "salesInvoiceLines", {"itemId", "lineType", "quantity", "discountAmount", "discountPercent", "amountExcludingTax", "netAmount", "shipmentDate"}, {"itemId", "lineType", "quantity", "discountAmount", "discountPercent", "amountExcludingTax", "netAmount", "shipmentDate"})
    in
    #"Expanded salesInvoiceLines"

  • hkusulja Profile Picture
    hkusulja 50 on at
    RE: Retrieve all Posted Sales Invoice Lines using API for Power BI

    Steven Renders  , i need for sales invoice, not purchase invoice, anyhow, as mentioned before, Dynamics365BusinessCentral connector is not available for online refresh nor Data Flow, therefore I think only way to use is using ODatav4 connector

  • Suggested answer
    Steven Renders Profile Picture
    Steven Renders 5,053 Super User 2024 Season 1 on at
    RE: Retrieve all Posted Sales Invoice Lines using API for Power BI

    You can do it using the standard APIs, here's an example:

    [View:/cfs-file/__key/communityserver-discussions-components-files/758/Purchases.pbix:320:240]

    7357.pi.png

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 75,942 Super User 2024 Season 2 on at
    RE: Retrieve all Posted Sales Invoice Lines using API for Power BI

    Hi, If you want to get both Purchase lines and Purchase Invoice Lines data, it is recommended to create a new page (a temporary table behind). when the Page is opened (OnOpenPage (Page) Trigger), insert data ofPurchase lines and Purchase Invoice Lines.
    Then publish it on the WebService.

    Hope this will help.

    Thanks

    ZHU

  • Guy-JM Profile Picture
    Guy-JM 95 on at
    RE: Retrieve all Posted Sales Invoice Lines using API for Power BI

    Page 133 is specific to Posted sales invoices and will not require additional filtering.

    pastedimage1645799846140v1.png

  • Hrvoje Kusulja Profile Picture
    Hrvoje Kusulja 370 on at
    RE: Retrieve all Posted Sales Invoice Lines using API for Power BI

    Yes, so somehow I need to write Power BI / OData query that will return from Lines (Page 133) filtered data only from Posted Sales Invoice Lines, to avoid necesery data loading in Power BI.

  • Suggested answer
    Guy-JM Profile Picture
    Guy-JM 95 on at
    RE: Retrieve all Posted Sales Invoice Lines using API for Power BI

    I have been able to get the lines into a dataflow extracting page 133 and the headers extracting page 132.

    pastedimage1645798005138v1.png

    In power Bi, create your new dataflow selecting odata data source. Paste your URL from the web services screen. The data transformation screen loads with the sales invoice line.

    pastedimage1645798457696v2.png

    From within the data transformation window, select Get data and add the odata source for the header table.

    You can then merge your queries as you require.

  • Suggested answer
    MahGah Profile Picture
    MahGah 15,433 on at
    RE: Retrieve all Posted Sales Invoice Lines using API for Power BI

    I wish I could help more but I have lack of knowledge. Steve has lots of knowledge for Power Bi 

    I tested with a few environments and published page 133 and when I connected to them I can see all sales line without any filter. I even opened that ODATA for a company with more than 100k sales invoice and all sales line where there. Yes in Power BI table looks like they are not there but if you click on load more then I could see them all (after long waiting for data to load). 

    pastedimage1645797937841v4.png

    pastedimage1645797588902v1.png

    pastedimage1645797640800v3.png

  • hkusulja Profile Picture
    hkusulja 50 on at
    RE: Retrieve all Posted Sales Invoice Lines using API for Power BI

    In Power BI i have tablice "sales invoice " and "sales invoce lines" , and they are connected. So i need to be able to list , sold Items (prices, quantities etc.), per Customer, invoice date etc. (from Sales invoice header).

    So i am currently missing query, how to retrieve all (not filtered) "posted sales invoice lines" via Power BI , for Dataflow.

  • Suggested answer
    MahGah Profile Picture
    MahGah 15,433 on at
    RE: Retrieve all Posted Sales Invoice Lines using API for Power BI

    Hi hkusulja  

    Maybe we are all missing your final goal. Can you explain to us what is final Power BI reports looks like? for example

    1) Do you want to list each invoice and list total sales value for each invoice? or

    2) You want user enter Invoice No in Power BI and then BI shows the total value of sales order or lines? Or

    3) You want to show total sales order value for specific date period? or

    4) etc

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,784 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,476 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans