Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
Anonymous
Not applicable

Knapsack Problem

Hi, 

 

I need to implement the Knapsack Problem.

I know there are many algorithms in Java code. It's possible to replicate it in PowerBI? If yes, how?

 

Here an example of what I need.

 

 

class Knapsack {

    public static void main(String[] args) throws Exception {

        int val[] = {10, 40, 30, 50};

        int wt[] = {5, 4, 6, 3};

        int W = 10;

        System.out.println(knapsack(val, wt, W));

   }



    public static int knapsack(int val[], int wt[], int W) {

        //Get the total number of items.

        //Could be wt.length or val.length. Doesn't matter

        int N = wt.length;


        //Create a matrix.

        //Items are in rows and weight at in columns +1 on each side

        int[][] V = new int[N + 1][W + 1];

        //What if the knapsack's capacity is 0 - Set

        //all columns at row 0 to be 0

        for (int col = 0; col <= W; col++) {            V[0][col] = 0;

        }



        //What if there are no items at home.

        //Fill the first row with 0
        for (int row = 0; row <= N; row++) {            V[row][0] = 0;
        }

        for (int item=1;item<=N;item++){


            //Let's fill the values row by row

            for (int weight=1;weight<=W;weight++){

                //Is the current items weight less

                //than or equal to running weight

                if (wt[item-1]<=weight){

//Given a weight, check if the value of the current

//item + value of the item that we could afford

//with the remaining weight is greater than the value

//without the current item itself                    V[item][weight]=Math.max (val[item-1]+V[item-1][weight-wt[item-1]], V[item-1][weight]);
                }

                else {

//If the current item's weight is more than the

//running weight, just carry forward the value

//without the current item                    V[item][weight]=V[item-1][weight];
                }
            }

        }

        //Printing the matrix

        for (int[] rows : V) {

            for (int col : rows) {

                System.out.format("%5d", col);
            }
            System.out.println();
        }

        return V[N][W];

    }

}

 

 

Thanks

Clara

1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @Anonymous ,

Maybe you can try these:

Creating Machine Learning models in Power BI

Open Source PowerBI REST API for Java

Rest api usage with Java.

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
Icey
Community Support
Community Support

Hi @Anonymous ,

Maybe you can try these:

Creating Machine Learning models in Power BI

Open Source PowerBI REST API for Java

Rest api usage with Java.

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.